nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
296 stars 60 forks source link

log file created but not written #38

Closed BastienDurel closed 2 years ago

BastienDurel commented 2 years ago

Hello,

I tried to use AddFile(file, options => {}) to add a filte logger to my logger initialization, but this did not work

The log file is created, but nothing is written into it.

I pushed a minimalist project here : https://git.geekwu.org/bastien/test_log

I also tried to use the loggerFactory.AddProvider(new FileLoggerProvider(file, true)); approch, but I got the same (non) result

What did I miss ?

VitaliyMF commented 2 years ago

What did I miss ?

problem is in the initLogger method:

           using ILoggerFactory loggerFactory =
                LoggerFactory.Create(builder =>

when method returns using calls loggerFactory.Dispose which finalizes all logging providers. For FileLoggerProvider Dispose just closes the file and nothing will be written after that.

Console logger works because of it internals - it doesn't close anything after dispose. However, disposing LoggerFactory and using loggers after that is a very wrong usage of the logging infrastructure.

BastienDurel commented 2 years ago

You're right, thank you. I copied this from the SimpleConsole documentation without knowing the using keyword implies disposition.

Should I keep a reference on the factory, or letting it leave the scope without being disposed is OK ?