Skip to content
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

Merged
merged 4 commits into from
Nov 16, 2024
Merged

Conversation

agehrke
Copy link
Contributor

@agehrke agehrke commented Nov 15, 2024

Currently SerilogLoggerProvider only implements IDisposable which means that sinks that implement IAsyncDisposable will not be disposed async when dispose=true is passed to AddSerilog().

In this PR I have opt'ed to only implement IAsyncDisposable for .NET 6 or later as we can't use the static Log.CloseAndFlushAsync() method on .NET Framework. When an existing ILogger is provided to AddSerilog() and dispose=true we will now try to call IAsyncDisposable.DisposeAsync() and fallback to IDisposable.Dispose().
If no existing ILogger is provided we will use Log.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 from MS..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.

Copy link
Member

@nblumhardt nblumhardt left a 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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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()
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I will address!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming changed

Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks great 👍

@nblumhardt nblumhardt merged commit 0584a5b into serilog:dev Nov 16, 2024
1 check passed
@nblumhardt nblumhardt mentioned this pull request Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants