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

Need to be able to specify dispose:true #22

Open
danielabbatt opened this issue Mar 7, 2018 · 2 comments
Open

Need to be able to specify dispose:true #22

danielabbatt opened this issue Mar 7, 2018 · 2 comments
Labels

Comments

@danielabbatt
Copy link

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)
);
@nblumhardt
Copy link
Member

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!

@nblumhardt nblumhardt added the bug label Aug 23, 2019
@Apollo3zehn
Copy link

Apollo3zehn commented May 22, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants