From 495b1a2379b5ab7ce799ad2fa42adcaa95017aed Mon Sep 17 00:00:00 2001 From: Christoph Bersch Date: Fri, 22 Oct 2021 09:02:03 +0200 Subject: [PATCH 01/15] Pass registered logger to DiagnosticContext The DiagnosticContext should use the logger, which is passed to or created by UseSerilog(). --- .../SerilogHostBuilderExtensions.cs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs index 7764a2b..086f810 100644 --- a/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs +++ b/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs @@ -80,7 +80,13 @@ public static IHostBuilder UseSerilog( collection.AddSingleton(services => new SerilogLoggerFactory(logger, dispose)); } - ConfigureServices(collection, logger); + if (logger != null) + { + // This won't (and shouldn't) take ownership of the logger. + collection.AddSingleton(logger); + } + bool useRegisteredLogger = logger != null; + ConfigureDiagnosticContext(collection, useRegisteredLogger); }); return builder; @@ -221,32 +227,26 @@ public static IHostBuilder UseSerilog( return factory; }); - - // Null is passed here because we've already (lazily) registered `ILogger` - ConfigureServices(collection, null); + + ConfigureDiagnosticContext(collection, preserveStaticLogger); }); return builder; } - static void ConfigureServices(IServiceCollection collection, ILogger logger) + static void ConfigureDiagnosticContext(IServiceCollection collection, bool useRegisteredLogger) { if (collection == null) throw new ArgumentNullException(nameof(collection)); - if (logger != null) - { - // This won't (and shouldn't) take ownership of the logger. - collection.AddSingleton(logger); - } - - // Registered to provide two services... - var diagnosticContext = new DiagnosticContext(logger); - + // Registered to provide two services... // Consumed by e.g. middleware - collection.AddSingleton(diagnosticContext); - + collection.AddSingleton(services => + { + ILogger logger = useRegisteredLogger ? services.GetRequiredService().Logger : null; + return new DiagnosticContext(logger); + }); // Consumed by user code - collection.AddSingleton(diagnosticContext); + collection.AddSingleton(services => services.GetRequiredService()); } } } From 31767708fa3cc8db6b1509c99aae36af2a1bd1b7 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 4 Nov 2021 15:32:55 +0100 Subject: [PATCH 02/15] Add IDiagnosticContext.SetException() --- .../Extensions/Hosting/DiagnosticContext.cs | 7 +++ .../Hosting/DiagnosticContextCollector.cs | 53 +++++++++++++++++++ .../IDiagnosticContext.cs | 19 ++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs index 7c12c19..44462ac 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContext.cs @@ -56,5 +56,12 @@ public void Set(string propertyName, object value, bool destructureObjects = fal collector.AddOrUpdate(property); } } + + /// + public void SetException(Exception exception) + { + var collector = AmbientDiagnosticContextCollector.Current; + collector?.SetException(exception); + } } } diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs index d1aba65..c7ab697 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs @@ -11,6 +11,7 @@ public sealed class DiagnosticContextCollector : IDisposable { readonly IDisposable _chainedDisposable; readonly object _propertiesLock = new object(); + Exception _exception; Dictionary _properties = new Dictionary(); /// @@ -38,6 +39,28 @@ public void AddOrUpdate(LogEventProperty property) } } + /// + /// Set an exception to include in the Serilog request log event. + /// + /// + /// Passing an exception to the diagnostic context is useful when unhandled exceptions are handled before reaching Serilog's + /// RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform + /// exceptions to ProblemDetails responses. + /// + /// + /// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception takes precedence.
+ /// If null is given, it clears any previously assigned exception. + ///
+ /// The exception to log. + public void SetException(Exception exception) + { + lock (_propertiesLock) + { + if (_properties == null) return; + _exception = exception; + } + } + /// /// Complete the context and retrieve the properties added to it, if any. This will /// stop collection and remove the collector from the original execution context and @@ -51,6 +74,36 @@ public bool TryComplete(out IEnumerable properties) { properties = _properties?.Values; _properties = null; + _exception = null; + Dispose(); + return properties != null; + } + } + + /// + /// Complete the context and retrieve the properties added to it, if any. This will + /// stop collection and remove the collector from the original execution context and + /// any of its children. + /// + /// The collected properties, or null if no collection is active. + /// The collected exception, or null if none has been collected or if no collection is active. + /// True if properties could be collected. + /// + /// This overload provides the exception collected by the diagnostic context, which is useful when unhandled exceptions are handled + /// before reaching Serilog's RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform + /// exceptions to ProblemDetails responses. + /// + /// + /// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception should take precedence.
+ ///
+ public bool TryComplete(out IEnumerable properties, out Exception exception) + { + lock (_propertiesLock) + { + properties = _properties?.Values; + exception = _exception; + _properties = null; + _exception = null; Dispose(); return properties != null; } diff --git a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs index 468fa84..cbdeced 100644 --- a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs +++ b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; + namespace Serilog { /// @@ -27,6 +29,21 @@ public interface IDiagnosticContext /// The property value. /// If true, the value will be serialized as structured /// data if possible; if false, the object will be recorded as a scalar or simple array. - void Set(string propertyName, object value, bool destructureObjects = false); + void Set(string propertyName, object value, bool destructureObjects = false); + + /// + /// Set an exception to include in the Serilog request log event. + /// + /// + /// Passing an exception to the diagnostic context is useful when unhandled exceptions are handled before reaching Serilog's + /// RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform + /// exceptions to ProblemDetails responses. + /// + /// + /// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception takes precedence.
+ /// If null is given, it clears any previously assigned exception. + ///
+ /// The exception to log. + void SetException(Exception exception); } } From 6f32cb4d34cae6e581abd43108dbb429b8c2d6ce Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 4 Nov 2021 18:53:03 +0100 Subject: [PATCH 03/15] Add tests --- .../DiagnosticContextTests.cs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test/Serilog.Extensions.Hosting.Tests/DiagnosticContextTests.cs b/test/Serilog.Extensions.Hosting.Tests/DiagnosticContextTests.cs index c32f9ea..c3fbb1d 100644 --- a/test/Serilog.Extensions.Hosting.Tests/DiagnosticContextTests.cs +++ b/test/Serilog.Extensions.Hosting.Tests/DiagnosticContextTests.cs @@ -18,6 +18,13 @@ public void SetIsSafeWhenNoContextIsActive() dc.Set(Some.String("name"), Some.Int32()); } + [Fact] + public void SetExceptionIsSafeWhenNoContextIsActive() + { + var dc = new DiagnosticContext(Some.Logger()); + dc.SetException(new Exception("test")); + } + [Fact] public async Task PropertiesAreCollectedInAnActiveContext() { @@ -39,6 +46,48 @@ public async Task PropertiesAreCollectedInAnActiveContext() Assert.False(collector.TryComplete(out _)); } + [Fact] + public void ExceptionIsCollectedInAnActiveContext() + { + var dc = new DiagnosticContext(Some.Logger()); + var collector = dc.BeginCollection(); + + var setException = new Exception("before collect"); + dc.SetException(setException); + + Assert.True(collector.TryComplete(out _, out var collectedException)); + Assert.Same(setException, collectedException); + } + + [Fact] + public void ExceptionIsNotCollectedAfterTryComplete() + { + var dc = new DiagnosticContext(Some.Logger()); + var collector = dc.BeginCollection(); + collector.TryComplete(out _, out _); + dc.SetException(new Exception(Some.String("after collect"))); + + var tryComplete2 = collector.TryComplete(out _, out var collectedException2); + + Assert.False(tryComplete2); + Assert.Null(collectedException2); + } + + [Fact] + public void ExceptionIsNotCollectedAfterDispose() + { + var dc = new DiagnosticContext(Some.Logger()); + var collector = dc.BeginCollection(); + collector.Dispose(); + + dc.SetException(new Exception("after dispose")); + + var tryComplete = collector.TryComplete(out _, out var collectedException); + + Assert.True(tryComplete); + Assert.Null(collectedException); + } + [Fact] public void ExistingPropertiesCanBeUpdated() { @@ -53,5 +102,18 @@ public void ExistingPropertiesCanBeUpdated() var scalar = Assert.IsType(prop.Value); Assert.Equal(20, scalar.Value); } + + [Fact] + public void ExistingExceptionCanBeUpdated() + { + var dc = new DiagnosticContext(Some.Logger()); + var collector = dc.BeginCollection(); + + dc.SetException(new Exception("ex1")); + dc.SetException(new Exception("ex2")); + + Assert.True(collector.TryComplete(out _, out var collectedException)); + Assert.Equal("ex2", collectedException.Message); + } } } From cb36e965e810d58822dbb23418f0d3f31b98c0fc Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 4 Nov 2021 20:20:00 +0100 Subject: [PATCH 04/15] Fix xmldoc --- .../Hosting/DiagnosticContextCollector.cs | 13 ++++--------- .../IDiagnosticContext.cs | 12 +++++------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs index c7ab697..abd96e9 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs @@ -68,6 +68,7 @@ public void SetException(Exception exception) ///
/// The collected properties, or null if no collection is active. /// True if properties could be collected. + /// public bool TryComplete(out IEnumerable properties) { lock (_propertiesLock) @@ -81,21 +82,15 @@ public bool TryComplete(out IEnumerable properties) } /// - /// Complete the context and retrieve the properties added to it, if any. This will + /// Complete the context and retrieve the properties and exception added to it, if any. This will /// stop collection and remove the collector from the original execution context and /// any of its children. /// /// The collected properties, or null if no collection is active. /// The collected exception, or null if none has been collected or if no collection is active. /// True if properties could be collected. - /// - /// This overload provides the exception collected by the diagnostic context, which is useful when unhandled exceptions are handled - /// before reaching Serilog's RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform - /// exceptions to ProblemDetails responses. - /// - /// - /// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception should take precedence.
- ///
+ /// + /// public bool TryComplete(out IEnumerable properties, out Exception exception) { lock (_propertiesLock) diff --git a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs index cbdeced..0c6ccdc 100644 --- a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs +++ b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs @@ -32,15 +32,13 @@ public interface IDiagnosticContext void Set(string propertyName, object value, bool destructureObjects = false); /// - /// Set an exception to include in the Serilog request log event. + /// Set the specified exception on the current diagnostic context. + ///

+ /// This is useful when unhandled exceptions do not reach Serilog.AspNetCore's RequestLoggingMiddleware, + /// such as when using Hellang.Middleware.ProblemDetails + /// to transform exceptions to ProblemDetails responses. ///
- /// - /// Passing an exception to the diagnostic context is useful when unhandled exceptions are handled before reaching Serilog's - /// RequestLoggingMiddleware. One example is using https://www.nuget.org/packages/Hellang.Middleware.ProblemDetails to transform - /// exceptions to ProblemDetails responses. - /// /// - /// If an unhandled exception reaches Serilog's RequestLoggingMiddleware, then the unhandled exception takes precedence.
/// If null is given, it clears any previously assigned exception. ///
/// The exception to log. From 66df9b740e45d9d558c03ae101fe7eab4e70e8f4 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 4 Nov 2021 20:22:00 +0100 Subject: [PATCH 05/15] Fix xmldoc --- src/Serilog.Extensions.Hosting/IDiagnosticContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs index 0c6ccdc..f7cf55f 100644 --- a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs +++ b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs @@ -34,7 +34,7 @@ public interface IDiagnosticContext /// /// Set the specified exception on the current diagnostic context. ///

- /// This is useful when unhandled exceptions do not reach Serilog.AspNetCore's RequestLoggingMiddleware, + /// This is useful when unhandled exceptions do not reach Serilog.AspNetCore.RequestLoggingMiddleware, /// such as when using Hellang.Middleware.ProblemDetails /// to transform exceptions to ProblemDetails responses. ///
From 668132ae972d7aa01ab699c95658f39845e87d36 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 4 Nov 2021 20:23:46 +0100 Subject: [PATCH 06/15] Reuse TryComplete overload --- .../Extensions/Hosting/DiagnosticContextCollector.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs index abd96e9..29d11c8 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs @@ -71,14 +71,7 @@ public void SetException(Exception exception) /// public bool TryComplete(out IEnumerable properties) { - lock (_propertiesLock) - { - properties = _properties?.Values; - _properties = null; - _exception = null; - Dispose(); - return properties != null; - } + return TryComplete(out properties, out _); } /// From c729baec7d33d18dae69c603cfb3459c55699a0f Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 9 Nov 2021 08:01:14 +1000 Subject: [PATCH 07/15] Dev version bump [skip ci] --- .../Serilog.Extensions.Hosting.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj index 375fc60..8d2a280 100644 --- a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj +++ b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj @@ -2,7 +2,7 @@ Serilog support for .NET Core logging in hosted services - 4.2.0 + 4.2.1 Microsoft;Serilog Contributors netstandard2.0;netstandard2.1 8 From f13731ace6ec8cce1aab72e771461e39cd7b92e9 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 27 Feb 2022 17:24:35 +0100 Subject: [PATCH 08/15] Fix xmldoc, add ObsoleteAttribute --- .../Extensions/Hosting/DiagnosticContextCollector.cs | 3 ++- src/Serilog.Extensions.Hosting/IDiagnosticContext.cs | 10 ++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs index 29d11c8..63c6339 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/DiagnosticContextCollector.cs @@ -40,7 +40,7 @@ public void AddOrUpdate(LogEventProperty property) } /// - /// Set an exception to include in the Serilog request log event. + /// Set the exception associated with the current diagnostic context. /// /// /// Passing an exception to the diagnostic context is useful when unhandled exceptions are handled before reaching Serilog's @@ -69,6 +69,7 @@ public void SetException(Exception exception) /// The collected properties, or null if no collection is active. /// True if properties could be collected. /// + [Obsolete("Replaced by TryComplete(out IEnumerable properties, out Exception exception).")] public bool TryComplete(out IEnumerable properties) { return TryComplete(out properties, out _); diff --git a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs index f7cf55f..79da237 100644 --- a/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs +++ b/src/Serilog.Extensions.Hosting/IDiagnosticContext.cs @@ -33,15 +33,13 @@ public interface IDiagnosticContext /// /// Set the specified exception on the current diagnostic context. - ///

- /// This is useful when unhandled exceptions do not reach Serilog.AspNetCore.RequestLoggingMiddleware, - /// such as when using Hellang.Middleware.ProblemDetails - /// to transform exceptions to ProblemDetails responses. ///
/// - /// If null is given, it clears any previously assigned exception. + /// This method is useful when unhandled exceptions do not reach Serilog.AspNetCore.RequestLoggingMiddleware, + /// such as when using Hellang.Middleware.ProblemDetails + /// to transform exceptions to ProblemDetails responses. /// - /// The exception to log. + /// The exception to log. If null is given, it clears any previously assigned exception. void SetException(Exception exception); } } From d1d7e176131e2e90898a91890c92044e1a0cc96d Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 5 Mar 2022 07:14:13 +1000 Subject: [PATCH 09/15] Bump major version, breaking changes in #56 --- .../Serilog.Extensions.Hosting.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj index 8d2a280..a1c9cd3 100644 --- a/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj +++ b/src/Serilog.Extensions.Hosting/Serilog.Extensions.Hosting.csproj @@ -2,7 +2,7 @@ Serilog support for .NET Core logging in hosted services - 4.2.1 + 5.0.0 Microsoft;Serilog Contributors netstandard2.0;netstandard2.1 8 From 22b8f094098f394709f79224d083cda2c69bd524 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 21 Mar 2022 13:50:42 +1000 Subject: [PATCH 10/15] Publish on `main` --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d9cd93c..adf6f87 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -15,11 +15,11 @@ deploy: api_key: secure: Q65rY+zaFWOhs8a9IVSaX4Go5HNcIlEXjEFWMB83Y325WE9aXzi0xzDDc0/fJDzk on: - branch: /^(master|dev)$/ + branch: /^(main|dev)$/ - provider: GitHub auth_token: secure: p4LpVhBKxGS5WqucHxFQ5c7C8cP74kbNB0Z8k9Oxx/PMaDQ1+ibmoexNqVU5ZlmX artifact: /Serilog.*\.nupkg/ tag: v$(appveyor_build_version) on: - branch: master + branch: main From b393f438ede6aafa3788c347beb3595e7371fc98 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 21 Mar 2022 13:51:10 +1000 Subject: [PATCH 11/15] Publish from `main` --- Build.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Build.ps1 b/Build.ps1 index 41fbf5d..cce9432 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -11,7 +11,7 @@ if(Test-Path .\artifacts) { $branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL]; $revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL]; -$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"] +$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "main" -and $revision -ne "local"] echo "build: Version suffix is $suffix" From aad7cb8b8adf68cf733b9daf7049a14052151540 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 5 Jul 2022 09:14:43 +1000 Subject: [PATCH 12/15] Attempt a quick dev build fix - worker image does not include 5.x SDKs --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 14a6148..cb771af 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { "allowPrerelease": false, - "version": "5.0.102", + "version": "6.0.100", "rollForward": "latestFeature" } } From d6b36c10bd30eabb1eec062f5c987fe812484ec6 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 5 Jul 2022 09:15:46 +1000 Subject: [PATCH 13/15] Find a supported SDK, take two... --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index cb771af..28db99e 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { "allowPrerelease": false, - "version": "6.0.100", + "version": "5.0.409", "rollForward": "latestFeature" } } From 03278e67e06f478aa2eb967f94eddf445b9b7339 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 9 Jul 2022 08:09:54 +1000 Subject: [PATCH 14/15] Switch to the VS2022 build worker and .NET 6 SDK --- appveyor.yml | 2 +- global.json | 2 +- .../Extensions/Hosting/ReloadableLogger.cs | 8 ++++++-- .../Serilog.Extensions.Hosting.Tests.csproj | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index adf6f87..27bd2ea 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: '{build}' skip_tags: true -image: Visual Studio 2019 +image: Visual Studio 2022 test: off build_script: - ps: ./Build.ps1 diff --git a/global.json b/global.json index 28db99e..d56cd08 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { "allowPrerelease": false, - "version": "5.0.409", + "version": "6.0.301", "rollForward": "latestFeature" } } diff --git a/src/Serilog.Extensions.Hosting/Extensions/Hosting/ReloadableLogger.cs b/src/Serilog.Extensions.Hosting/Extensions/Hosting/ReloadableLogger.cs index 5c95a55..6ec1d37 100644 --- a/src/Serilog.Extensions.Hosting/Extensions/Hosting/ReloadableLogger.cs +++ b/src/Serilog.Extensions.Hosting/Extensions/Hosting/ReloadableLogger.cs @@ -368,9 +368,13 @@ public bool BindProperty(string propertyName, object value, bool destructureObje [MethodImpl(MethodImplOptions.AggressiveInlining)] (ILogger, bool) UpdateForCaller(ILogger root, ILogger cached, IReloadableLogger caller, out ILogger newRoot, out ILogger newCached, out bool frozen) { + // Synchronization on `_sync` is not required in this method; it will be called without a lock + // if `_frozen` and under a lock if not. + if (_frozen) { - // If we're frozen, then the caller hasn't observed this yet and should update. + // If we're frozen, then the caller hasn't observed this yet and should update. We could optimize a little here + // and only signal an update if the cached logger is stale (as per the next condition below). newRoot = _logger; newCached = caller.ReloadLogger(); frozen = true; @@ -384,7 +388,7 @@ public bool BindProperty(string propertyName, object value, bool destructureObje frozen = false; return (cached, false); } - + newRoot = _logger; newCached = caller.ReloadLogger(); frozen = false; diff --git a/test/Serilog.Extensions.Hosting.Tests/Serilog.Extensions.Hosting.Tests.csproj b/test/Serilog.Extensions.Hosting.Tests/Serilog.Extensions.Hosting.Tests.csproj index 1601189..c964995 100644 --- a/test/Serilog.Extensions.Hosting.Tests/Serilog.Extensions.Hosting.Tests.csproj +++ b/test/Serilog.Extensions.Hosting.Tests/Serilog.Extensions.Hosting.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net5.0;net4.8 + netcoreapp3.1;net6.0;net4.8 Serilog.Extensions.Hosting.Tests ../../assets/Serilog.snk true From 3af92b5efe224378224a85c7ff8dbc51280c6b6d Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Sat, 9 Jul 2022 08:13:48 +1000 Subject: [PATCH 15/15] Target 6.0.300, roll forwards --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index d56cd08..af5a328 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "sdk": { "allowPrerelease": false, - "version": "6.0.301", + "version": "6.0.300", "rollForward": "latestFeature" } }