From 05f7498496bb673946927ae81fb27ae970db1ad2 Mon Sep 17 00:00:00 2001 From: Andreas Gehrke Date: Fri, 15 Nov 2024 13:28:54 +0100 Subject: [PATCH 1/2] Update docs to help people use async flushing/disposal --- README.md | 4 ++-- samples/Sample/Program.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4b5ae29..a1828f6 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ try var app = builder.Build(); app.MapGet("/", () => "Hello World!"); - app.Run(); + await app.RunAsync(); } catch (Exception ex) { @@ -44,7 +44,7 @@ catch (Exception ex) } finally { - Log.CloseAndFlush(); + await Log.CloseAndFlushAsync(); } ``` diff --git a/samples/Sample/Program.cs b/samples/Sample/Program.cs index 7d334df..10cae98 100644 --- a/samples/Sample/Program.cs +++ b/samples/Sample/Program.cs @@ -65,5 +65,5 @@ } finally { - Log.CloseAndFlush(); + await Log.CloseAndFlushAsync(); } From ec7c39fb0fad0148fbef8fa0cb95e3e523aec8c8 Mon Sep 17 00:00:00 2001 From: Andreas Gehrke Date: Fri, 15 Nov 2024 13:30:04 +0100 Subject: [PATCH 2/2] implement a test that shows logger is not disposed asynchronously. --- .../SerilogWebHostBuilderExtensionsTests.cs | 14 ++++++++++++++ .../Support/DisposeTrackingLogger.cs | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/test/Serilog.AspNetCore.Tests/SerilogWebHostBuilderExtensionsTests.cs b/test/Serilog.AspNetCore.Tests/SerilogWebHostBuilderExtensionsTests.cs index 61ea07a..ba6ed56 100644 --- a/test/Serilog.AspNetCore.Tests/SerilogWebHostBuilderExtensionsTests.cs +++ b/test/Serilog.AspNetCore.Tests/SerilogWebHostBuilderExtensionsTests.cs @@ -38,6 +38,20 @@ public async Task DisposeShouldBeHandled(bool dispose) Assert.Equal(dispose, logger.IsDisposed); } + + [Fact] + public async Task DisposeAsyncShouldBeHandled() + { + var logger = new DisposeTrackingLogger(); + await using (var web = Setup(logger, dispose: true)) + { + await web.CreateClient().GetAsync("/"); + } + + Assert.Multiple( + () => Assert.True(logger.IsDisposedAsync), + () => Assert.False(logger.IsDisposed)); + } [Fact] public async Task RequestLoggingMiddlewareShouldEnrich() diff --git a/test/Serilog.AspNetCore.Tests/Support/DisposeTrackingLogger.cs b/test/Serilog.AspNetCore.Tests/Support/DisposeTrackingLogger.cs index 14278ef..c6cfebc 100644 --- a/test/Serilog.AspNetCore.Tests/Support/DisposeTrackingLogger.cs +++ b/test/Serilog.AspNetCore.Tests/Support/DisposeTrackingLogger.cs @@ -4,9 +4,10 @@ namespace Serilog.AspNetCore.Tests.Support; -sealed class DisposeTrackingLogger : ILogger, IDisposable +sealed class DisposeTrackingLogger : ILogger, IDisposable, IAsyncDisposable { public bool IsDisposed { get; private set; } + public bool IsDisposedAsync { get; private set; } public ILogger ForContext(ILogEventEnricher enricher) { @@ -352,4 +353,10 @@ public void Dispose() { IsDisposed = true; } + + public ValueTask DisposeAsync() + { + IsDisposedAsync = true; + return default; + } } \ No newline at end of file