-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from agehrke/asyncdispose
Async disposal on .NET 6 or later
- Loading branch information
Showing
4 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Xunit; | ||
|
||
namespace Serilog.Extensions.Logging.Tests; | ||
|
||
public class DisposeTests | ||
{ | ||
private readonly DisposableSink _sink; | ||
private readonly Logger _serilogLogger; | ||
|
||
public DisposeTests() | ||
{ | ||
_sink = new DisposableSink(); | ||
_serilogLogger = new LoggerConfiguration() | ||
.WriteTo.Sink(_sink) | ||
.CreateLogger(); | ||
} | ||
|
||
[Fact] | ||
public void DisposesProviderWhenDisposeIsTrue() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddLogging(builder => builder.AddSerilog(logger: _serilogLogger, dispose: true)) | ||
.BuildServiceProvider(); | ||
|
||
// Get a logger so that we ensure SerilogLoggerProvider is created | ||
var logger = services.GetRequiredService<ILogger<DisposeTests>>(); | ||
logger.LogInformation("Hello, world!"); | ||
|
||
services.Dispose(); | ||
Assert.True(_sink.DisposeCalled); | ||
Assert.False(_sink.DisposeAsyncCalled); | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
[Fact] | ||
public async Task DisposesProviderAsyncWhenDisposeIsTrue() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddLogging(builder => builder.AddSerilog(logger: _serilogLogger, dispose: true)) | ||
.BuildServiceProvider(); | ||
|
||
// Get a logger so that we ensure SerilogLoggerProvider is created | ||
var logger = services.GetRequiredService<ILogger<DisposeTests>>(); | ||
logger.LogInformation("Hello, world!"); | ||
|
||
await services.DisposeAsync(); | ||
Assert.False(_sink.DisposeCalled); | ||
Assert.True(_sink.DisposeAsyncCalled); | ||
} | ||
#endif | ||
|
||
private sealed class DisposableSink : ILogEventSink, IDisposable, IAsyncDisposable | ||
{ | ||
public bool DisposeAsyncCalled { get; private set; } | ||
public bool DisposeCalled { get; private set; } | ||
|
||
public void Dispose() => DisposeCalled = true; | ||
public ValueTask DisposeAsync() | ||
{ | ||
DisposeAsyncCalled = true; | ||
return default; | ||
} | ||
|
||
public void Emit(LogEvent logEvent) | ||
{ | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
test/Serilog.Extensions.Logging.Tests/SerilogLoggingBuilderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog.Extensions.Logging.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Extensions.Logging.Tests; | ||
|
||
public class SerilogLoggingBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddSerilogMustRegisterAnILoggerProvider() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddLogging(builder => { builder.AddSerilog(); }) | ||
.BuildServiceProvider(); | ||
|
||
var loggerProviders = services.GetServices<ILoggerProvider>(); | ||
Assert.Contains(loggerProviders, provider => provider is SerilogLoggerProvider); | ||
} | ||
|
||
[Fact] | ||
public void AddSerilogMustRegisterAnILoggerProviderThatForwardsLogsToStaticSerilogLogger() | ||
{ | ||
var sink = new SerilogSink(); | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Sink(sink) | ||
.CreateLogger(); | ||
|
||
var services = new ServiceCollection() | ||
.AddLogging(builder => { builder.AddSerilog(); }) | ||
.BuildServiceProvider(); | ||
|
||
var logger = services.GetRequiredService<ILogger<SerilogLoggingBuilderExtensionsTests>>(); | ||
logger.LogInformation("Hello, world!"); | ||
|
||
Assert.Single(sink.Writes); | ||
} | ||
|
||
[Fact] | ||
public void AddSerilogMustRegisterAnILoggerProviderThatForwardsLogsToProvidedLogger() | ||
{ | ||
var sink = new SerilogSink(); | ||
var serilogLogger = new LoggerConfiguration() | ||
.WriteTo.Sink(sink) | ||
.CreateLogger(); | ||
|
||
var services = new ServiceCollection() | ||
.AddLogging(builder => { builder.AddSerilog(logger: serilogLogger); }) | ||
.BuildServiceProvider(); | ||
|
||
var logger = services.GetRequiredService<ILogger<SerilogLoggingBuilderExtensionsTests>>(); | ||
logger.LogInformation("Hello, world!"); | ||
|
||
Assert.Single(sink.Writes); | ||
} | ||
} |