-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async disposal on .NET 6 or later #262
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just had some minor/trivial nitpicks, otherwise looks good to go.
@@ -14,13 +15,19 @@ namespace Serilog.Extensions.Logging; | |||
/// </summary> | |||
[ProviderAlias("Serilog")] | |||
public class SerilogLoggerProvider : ILoggerProvider, ILogEventEnricher, ISupportExternalScope | |||
#if NET6_0_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Serilog projects define constants for each conditional feature centrally in the CSPROJ, e.g.:
https://github.com/serilog/serilog/blob/dev/src/Serilog/Serilog.csproj
Could we define FEATURE_ASYNCDISPOSABLE
and use it in place of the version constraints in the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, dooh. Sorry about that. I had noticed that the Serilog core code base used such flags, but forgot to check this code. I will address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have defined FEATURE_ASYNCDISPOSABLE now
} | ||
|
||
[Fact] | ||
public void AddSerilog_must_dispose_the_provider_when_dispose_is_true() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Serilog codebases use PascalCaseForTestNames()
; shortening this to something like DisposesProviderWhenDisposeIsTrue()
would help make the scheme work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I will address!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks great 👍
Currently
SerilogLoggerProvider
only implementsIDisposable
which means that sinks that implementIAsyncDisposable
will not be disposed async whendispose=true
is passed toAddSerilog()
.In this PR I have opt'ed to only implement
IAsyncDisposable
for .NET 6 or later as we can't use the staticLog.CloseAndFlushAsync()
method on .NET Framework. When an existingILogger
is provided toAddSerilog()
anddispose=true
we will now try to callIAsyncDisposable.DisposeAsync()
and fallback toIDisposable.Dispose()
.If no existing
ILogger
is provided we will useLog.CloseAndFlushAsync()
for async disposal.The PR also includes tests of the disposal functionality, as well as some more high level "functional" tests of the functionality implemented in
AddSerilog()
, mainly forwarding of logs fromMS..Extensions.ILogger<T>
to Serilog sink.This change is related to a similar change I made in App Insights sink, see serilog-contrib/serilog-sinks-applicationinsights#228.