diff --git a/Build.ps1 b/Build.ps1 index a674b52..431f4e2 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -1,5 +1,7 @@ echo "build: Build started" +$env:Path = "$pwd/.dotnetcli;$env:Path" + Push-Location $PSScriptRoot if(Test-Path .\artifacts) { @@ -26,7 +28,7 @@ foreach ($src in ls src/*) { & dotnet pack -c Release --include-source -o ..\..\artifacts } - if($LASTEXITCODE -ne 0) { exit 1 } + if($LASTEXITCODE -ne 0) { throw "build failed" } Pop-Location } @@ -37,7 +39,7 @@ foreach ($test in ls test/*.PerformanceTests) { echo "build: Building performance test project in $test" & dotnet build -c Release - if($LASTEXITCODE -ne 0) { exit 2 } + if($LASTEXITCODE -ne 0) { throw "test failed" } Pop-Location } @@ -48,7 +50,7 @@ foreach ($test in ls test/*.Tests) { echo "build: Testing project in $test" & dotnet test -c Release - if($LASTEXITCODE -ne 0) { exit 3 } + if($LASTEXITCODE -ne 0) { throw "test failed" } Pop-Location } diff --git a/Setup.ps1 b/Setup.ps1 new file mode 100644 index 0000000..aa90b5b --- /dev/null +++ b/Setup.ps1 @@ -0,0 +1,9 @@ +$ErrorActionPreference = "Stop" + +$RequiredDotnetVersion = $(cat ./global.json | convertfrom-json).sdk.version + +New-Item -ItemType Directory -Force "./build/" | Out-Null + +Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./build/installcli.ps1" +& ./build/installcli.ps1 -InstallDir "$pwd/.dotnetcli" -NoPath -Version $RequiredDotnetVersion +if ($LASTEXITCODE) { throw ".NET install failed" } diff --git a/appveyor.yml b/appveyor.yml index 3e4a484..ae5bee4 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,9 +2,10 @@ version: '{build}' skip_tags: true image: Visual Studio 2022 install: - - ps: mkdir -Force ".\build\" | Out-Null +- pwsh: ./Setup.ps1 +- pwsh: mkdir -Force ".\build\" | Out-Null build_script: -- ps: ./Build.ps1 +- pwsh: ./Build.ps1 test: off artifacts: - path: artifacts/Serilog.*.nupkg diff --git a/global.json b/global.json new file mode 100644 index 0000000..5ce8495 --- /dev/null +++ b/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "8.0.100" + } +} diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index ef89f43..951b742 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 Sample Exe enable @@ -12,9 +12,9 @@ - - - + + + diff --git a/serilog-extensions-logging.sln b/serilog-extensions-logging.sln index 9e91f73..776ae3c 100644 --- a/serilog-extensions-logging.sln +++ b/serilog-extensions-logging.sln @@ -24,6 +24,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9 Directory.Build.targets = Directory.Build.targets README.md = README.md assets\Serilog.snk = assets\Serilog.snk + build.sh = build.sh + Setup.ps1 = Setup.ps1 + global.json = global.json EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Benchmarks", "test\Serilog.Extensions.Logging.Benchmarks\Serilog.Extensions.Logging.Benchmarks.csproj", "{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}" diff --git a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs index edcef30..06efe62 100644 --- a/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs +++ b/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs @@ -8,6 +8,7 @@ using System.Reflection; using Serilog.Debugging; using System.Collections.Concurrent; +using System.Diagnostics; namespace Serilog.Extensions.Logging; @@ -156,8 +157,12 @@ LogEvent PrepareWrite(LogEventLevel level, EventId eventId, TState state if (eventId.Id != 0 || eventId.Name != null) properties.Add(CreateEventIdProperty(eventId)); + var (traceId, spanId) = Activity.Current is { } activity ? + (activity.TraceId, activity.SpanId) : + (default(ActivityTraceId), default(ActivitySpanId)); + var parsedTemplate = MessageTemplateParser.Parse(messageTemplate ?? ""); - return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties); + return new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, properties, traceId, spanId); } static object? AsLoggableValue(TState state, Func? formatter) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 33d764b..c37866f 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -3,11 +3,11 @@ Low-level Serilog provider for Microsoft.Extensions.Logging - 7.0.0 + 8.0.0 Microsoft;Serilog Contributors - net462;netstandard2.0;netstandard2.1;net6.0;net7.0 + net462;netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0 true serilog;Microsoft.Extensions.Logging serilog-extension-nuget.png @@ -29,9 +29,9 @@ - + - + diff --git a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj index 24fdb78..155847e 100644 --- a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj +++ b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable @@ -10,10 +10,10 @@ - - - - + + + + diff --git a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj index 94c27dc..7c1a2f8 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -1,19 +1,23 @@ - net7.0;net472 + net8.0;net48 enable + + $(DefineConstants);FORCE_W3C_ACTIVITY_ID + + - - - - + + + + diff --git a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs index 94da8bb..179fd56 100644 --- a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs +++ b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections; +using System.Diagnostics; using Serilog.Events; using Microsoft.Extensions.Logging; using Serilog.Debugging; @@ -138,11 +139,11 @@ public void LogsCorrectMessage() logger.Log(LogLevel.Information, 0, null!, null!, null!); logger.Log(LogLevel.Information, 0, TestMessage, null!, null!); - logger.Log(LogLevel.Information, 0, null!, null!, (_, __) => TestMessage); + logger.Log(LogLevel.Information, 0, null!, null!, (_, _) => TestMessage); Assert.Equal(3, sink.Writes.Count); - Assert.Equal(1, sink.Writes[0].Properties.Count); + Assert.Single(sink.Writes[0].Properties); Assert.Empty(sink.Writes[0].RenderMessage()); Assert.Equal(2, sink.Writes[1].Properties.Count); @@ -308,7 +309,7 @@ public void WhenDisposeIsTrueProvidedLoggerIsDisposed() } [Fact] - public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate() + public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInMessageTemplate() { var (logger, sink) = SetUp(LogLevel.Trace); @@ -328,7 +329,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate } [Fact] - public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary() + public void BeginScopeDestructuresObjectsWhenCapturingOperatorIsUsedInDictionary() { var (logger, sink) = SetUp(LogLevel.Trace); @@ -348,7 +349,7 @@ public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary() } [Fact] - public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate() + public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInMessageTemplate() { var (logger, sink) = SetUp(LogLevel.Trace); @@ -362,7 +363,7 @@ public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate } [Fact] - public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInDictionary() + public void BeginScopeDoesNotModifyKeyWhenCapturingOperatorIsNotUsedInDictionary() { var (logger, sink) = SetUp(LogLevel.Trace); @@ -466,7 +467,10 @@ public void MismatchedMessageTemplateParameterCountIsHandled() { var (logger, sink) = SetUp(LogLevel.Trace); +#pragma warning disable CA2017 + // ReSharper disable once StructuredMessageTemplateProblem logger.LogInformation("Some test message with {Two} {Properties}", "OneProperty"); +#pragma warning restore CA2017 Assert.Empty(sink.Writes); } @@ -475,7 +479,7 @@ public void MismatchedMessageTemplateParameterCountIsHandled() public void ExceptionFromAuditSinkIsUnhandled() { var serilogLogger = new LoggerConfiguration() - .AuditTo.Sink(new MySink()) + .AuditTo.Sink(new UnimplementedSink()) .CreateLogger(); var provider = new SerilogLoggerProvider(serilogLogger); @@ -486,11 +490,38 @@ public void ExceptionFromAuditSinkIsUnhandled() Assert.Equal("Oops", ex.InnerException.Message); } - private class MySink : ILogEventSink + class UnimplementedSink : ILogEventSink { public void Emit(LogEvent logEvent) { throw new NotImplementedException("Oops"); } } + + [Fact] + public void TraceAndSpanIdsAreCaptured() + { +#if FORCE_W3C_ACTIVITY_ID + Activity.DefaultIdFormat = ActivityIdFormat.W3C; + Activity.ForceDefaultIdFormat = true; +#endif + + using var listener = new ActivityListener(); + listener.ShouldListenTo = _ => true; + listener.Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllData; + + ActivitySource.AddActivityListener(listener); + + var source = new ActivitySource("test.activity", "1.0.0"); + using var activity = source.StartActivity(); + Assert.NotNull(Activity.Current); + + var (logger, sink) = SetUp(LogLevel.Trace); + logger.LogInformation("Hello trace and span!"); + + var evt = Assert.Single(sink.Writes); + + Assert.Equal(Activity.Current.TraceId, evt.TraceId); + Assert.Equal(Activity.Current.SpanId, evt.SpanId); + } }