Need to be able to specify dispose:true
danielabbatt opened this issue · comments
I tried to use this in a console application but found that if I logged and exited immediately then the log file was not flushed, there was no opportunity to specify the dispose.
I ended up doing the below in the end.
// Set up Serilog
Log.Logger = new LoggerConfiguration()
.WriteTo.RollingFile(logFilename)
.CreateLogger();
// Add logging
services.AddSingleton(
new LoggerFactory()
.AddSerilog(dispose:true)
.AddConsole(loggingConfiguration)
.AddDebug(LogLevel.Trace)
);
Thanks for the heads-up, Daniel. I think dispose: true
should be the default/only supported option using this package - if you or anyone out there would like to investigate and/or send a PR, any help here would be appreciated. Thanks!
I had a similar problem that I could not access the log file despite disposing the logger factory.
I first tried this (not working):
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddFile(path to file);
});
I was then digging around the .NET source code and found this and this.
The code in the first link is called when using the ILoggingBuilder
extension methods. I don't know why but disposing: false
is enforced there, so my first code snipped could not work.
The code in the second link is called when using the ILoggerFactory
extension methods. Here, disposing: true
is enforced.
With these findings I changed the code to:
using var loggerFactory = LoggerFactory.Create(builder => { });
loggerFactory.AddFile(path to file);
And now everything is working fine. When the method finishes, the file handle is disposed properly.