nreco / logging

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

Do log entries always get flushed to file? #63

Closed Marv51 closed 8 months ago

Marv51 commented 8 months ago

I am investigating a crash in our app by analyzing the logging just before the crash. That got me wondering: Is the last logging message reliable? If an exception occurs, will the logging queue be flushed completely?

I could not find any method to manually flush the logging messages to the file. Is that because it is not necessary?

VitaliyMF commented 8 months ago

That got me wondering: Is the last logging message reliable? If an exception occurs, will the logging queue be flushed completely?

It seems only answer I can get is: "it depends".

From one side, FileLoggerProvider is implemented in a way that should contribute to this desired behavior: in FileLoggerProvider.ProcessQueue where log messages queue is processed, then the last log entry is written to a file an explicit Flush is called (see FileWriter.WriteMessage(string message, bool flush). Also, on FileLoggerProvider.Dispose opened file is closed and before that all remaining messages are queue are processed - this should guarantee that all last log entries are written to a file.

However, it could be an exception in your app that causes immedia crush and in this case it might be possible situation that some log entries that were in the queue are either not processed (=not written to a file) or flush wasn't called because of the same reason.

I could not find any method to manually flush the logging messages to the file. Is that because it is not necessary?

Normally this is not needed:

Marv51 commented 8 months ago

Hey, thanks for the details @VitaliyMF.

Your library performed perfectly. The problem we had is super weird, but with NReco.Logging we narrowed it down to the exact line that was causing the crash.

In this case, the answer to my question "Do log entries always get flushed to file?" was absolutely "YES".

Thanks for the great work on this library.