SamDevx
SamDevx

Reputation: 2378

How to dispose of Serilog 2.0 correctly in ASP.NET Core application?

According to documentation, when an application is shutting down, it is recommended to dispose of Serilog using either

 Log.CloseAndFlush();

or in case you have an instance variable to .CreateLogger(), call its

.Dispose();

When building ASP.NET Core app in Visual Studio, it generates Program and Startup classes, neither of which implements IDisposable. Are we supposed to add this interface to Program so that we have a way to dispose Serilog directly?

Upvotes: 6

Views: 3971

Answers (1)

Kiran
Kiran

Reputation: 57979

As far as I know, ASP.NET calls into dispose of the logger providers and so its the responsibility of the individual providers to do any clean up.

But looks like Serilog's logger provider does not do anything in its dispose method: https://github.com/serilog/serilog-extensions-logging/blob/master/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs

I would suggest to post a question on the Serilog's repo.

Upvotes: 2

Related Questions