From 54585676b0d1c5c6c1c50092da66d0bd3b8a8f13 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 3 Oct 2023 14:48:57 +1000 Subject: [PATCH 1/7] Capture trace and span ids for Serilog 3.1 --- .../Extensions/Logging/SerilogLogger.cs | 7 +++- .../Serilog.Extensions.Logging.csproj | 2 +- .../SerilogLoggerTests.cs | 40 +++++++++++++++---- 3 files changed, 40 insertions(+), 9 deletions(-) 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..7fb4e74 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -29,7 +29,7 @@ - + diff --git a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs index 94da8bb..597127e 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,7 +139,7 @@ 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); @@ -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,33 @@ 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() + { + 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); + } } From 6585051952c81b1df07af0c867d112ac0c4b53ba Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 3 Oct 2023 15:13:56 +1000 Subject: [PATCH 2/7] Force W3C activity ids on .NET 4.x --- .../Serilog.Extensions.Logging.Benchmarks.csproj | 2 +- .../Serilog.Extensions.Logging.Tests.csproj | 6 +++++- .../Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs | 7 ++++++- 3 files changed, 12 insertions(+), 3 deletions(-) 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..dcaf754 100644 --- a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj +++ b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj @@ -12,7 +12,7 @@ - + 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..88d54b2 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -4,6 +4,10 @@ net7.0;net472 enable + + + $(DefineConstants);FORCE_W3C_ACTIVITY_ID + @@ -12,7 +16,7 @@ - + diff --git a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs index 597127e..179fd56 100644 --- a/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs +++ b/test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs @@ -143,7 +143,7 @@ public void LogsCorrectMessage() 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); @@ -501,6 +501,11 @@ public void Emit(LogEvent logEvent) [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; From 3f1ccda34bfb0e833dde82d496f05e1233ab1a04 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 3 Oct 2023 15:32:46 +1000 Subject: [PATCH 3/7] Unmatched apostrophe :-) --- .../Serilog.Extensions.Logging.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 88d54b2..d7df0d9 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -5,7 +5,7 @@ enable - + $(DefineConstants);FORCE_W3C_ACTIVITY_ID From 24d858321e846e472c2702a670971b4f633f223e Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 11 Oct 2023 08:44:50 +1000 Subject: [PATCH 4/7] Bump version to 7.0.1; forgot earlier that the versioning policy only covers major and minor. --- .../Serilog.Extensions.Logging.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 7fb4e74..2b17b58 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -3,7 +3,7 @@ Low-level Serilog provider for Microsoft.Extensions.Logging - 7.0.0 + 7.0.1 Microsoft;Serilog Contributors From 6e71b3381dd65c0b6a23b228a09973989ce5e249 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 14 Oct 2023 13:30:38 +1000 Subject: [PATCH 5/7] Update to .NET 8 RC2 --- Build.ps1 | 6 +++--- Setup.ps1 | 11 +++++++++++ appveyor.yml | 5 +++-- global.json | 5 +++++ samples/Sample/Sample.csproj | 6 +++--- serilog-extensions-logging.sln | 3 +++ .../Serilog.Extensions.Logging.csproj | 6 +++--- .../Serilog.Extensions.Logging.Benchmarks.csproj | 10 +++++----- .../Serilog.Extensions.Logging.Tests.csproj | 12 ++++++------ 9 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 Setup.ps1 create mode 100644 global.json diff --git a/Build.ps1 b/Build.ps1 index a674b52..b20a19e 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -26,7 +26,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 +37,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 +48,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..6812767 --- /dev/null +++ b/Setup.ps1 @@ -0,0 +1,11 @@ +$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" } + +$env:Path = "$pwd/.dotnetcli;$env:Path" 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..3ecc5db --- /dev/null +++ b/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version": "8.0.100-rc.2.23502.2" + } +} diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index ef89f43..16b9ecb 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 Sample Exe enable @@ -12,8 +12,8 @@ - - + + 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/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 2b17b58..285ab7b 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.1 + 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 @@ -31,7 +31,7 @@ - + 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 dcaf754..7d85956 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 d7df0d9..4dd1afb 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -1,11 +1,11 @@ - net7.0;net472 + net8.0;net48 enable - - + + $(DefineConstants);FORCE_W3C_ACTIVITY_ID @@ -14,9 +14,9 @@ - - - + + + From 80064f8670737b74db738fafb3d46a9843f130c2 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 16 Oct 2023 18:37:27 +1000 Subject: [PATCH 6/7] Set SDK path from build script --- Build.ps1 | 2 ++ Setup.ps1 | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Build.ps1 b/Build.ps1 index b20a19e..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) { diff --git a/Setup.ps1 b/Setup.ps1 index 6812767..aa90b5b 100644 --- a/Setup.ps1 +++ b/Setup.ps1 @@ -7,5 +7,3 @@ 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" } - -$env:Path = "$pwd/.dotnetcli;$env:Path" From 1fa32a50134131827c676ab6a80287e57b503a58 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Wed, 15 Nov 2023 08:35:07 +1000 Subject: [PATCH 7/7] Update to .NET 8 RTM and Serilog 3.1.1 --- global.json | 2 +- samples/Sample/Sample.csproj | 6 +++--- .../Serilog.Extensions.Logging.csproj | 4 ++-- .../Serilog.Extensions.Logging.Benchmarks.csproj | 6 +++--- .../Serilog.Extensions.Logging.Tests.csproj | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/global.json b/global.json index 3ecc5db..5ce8495 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "8.0.100-rc.2.23502.2" + "version": "8.0.100" } } diff --git a/samples/Sample/Sample.csproj b/samples/Sample/Sample.csproj index 16b9ecb..951b742 100644 --- a/samples/Sample/Sample.csproj +++ b/samples/Sample/Sample.csproj @@ -12,9 +12,9 @@ - - - + + + diff --git a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj index 285ab7b..c37866f 100644 --- a/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj +++ b/src/Serilog.Extensions.Logging/Serilog.Extensions.Logging.csproj @@ -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 7d85956..155847e 100644 --- a/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj +++ b/test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj @@ -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 4dd1afb..7c1a2f8 100644 --- a/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj +++ b/test/Serilog.Extensions.Logging.Tests/Serilog.Extensions.Logging.Tests.csproj @@ -14,10 +14,10 @@ - + - - + +