Add SynchronousWrite option for immediate log persistence#38
Add SynchronousWrite option for immediate log persistence#38icnocop wants to merge 1 commit intoadams85:masterfrom
Conversation
Introduce SynchronousWrite setting to file logger configuration, allowing log entries to be written directly to file on the calling thread and bypassing the background queue. This ensures logs are persisted before ILogger.Log returns, useful for crash diagnostics. Updated processor, settings, and documentation. Added unit tests for both global and per-file SynchronousWrite behavior.
|
First of all, thanks for the contribution. However, at a first glance, I'm not convinced that this a good idea: such a block-waiting operation mode is a big performance killer. Could you tell me more about your application that would require this? With what kind of crashes would this help? Please note that even though the current implementation uses background queues, it makes sure that the queues are written to disk when the logger gets disposed. So, if your application handles shutdown correctly, you shouldn't really end up with missing logs. (There is a completion timeout though, which is set to 1.5 sec by default. However, this can be increased as needed.) AFAICS, the only cases where you can actually lose some logs are non-catchable exceptions like The other benefit of the proposed mode I can imagine is that it would enable applying backpressure in an application that produces a huge amount of logs. Is this maybe what you want to achieve? |
Right, it's not a good idea to enable this by default in production. This setting is disabled by default.
Any exception that is not caught and cause the application to crash, or worse, causes a BSOD, when calling low-level Win32 APIs which perform hardware I/O for example. When a BSOD occurs for example, the latest log messages are not flushed to disk and so it makes it harder to troubleshoot the issue without the latest log messages.
This feature is specifically for when the application doesn't gracefully shutdown, as a result of a BSOD for example.
This internal value doesn't seem like it satisfies the requirements. For example, I can't set it in appsettings.json, and I'm not sure a timeout value would achieve the desired results.
When
No. The logs don't have to be huge. The log messages can be filtered by their category. I could imagine another case where enabling Thank you for your consideration. |
Another option is to set it to
You mean "...persist before a BSOD for example", right? I'd be surprised if anything was persisted after you got a BSOD.
Okay, I think now I understand what you are onto. This would be a kind of temporary diagnostic mode that could help you track down nasty crashes that brings down the process or even the OS. After all, I see some value in this. However, the implementation would need some more work. E.g.:
A further problem that there may be OS-level buffering. So in this mode we probably want to write through such buffers, e.g., using
However, as you're surely aware, locks result in queuing too. It's just a blocking queue, not an asynchronous one. So, if possible, I'd prefer to implement this on top of the queues (channels) we already have in place. A pattern like this might work: However, I'm not very comfortable with blocking over asynchronous code. That's a recipe for disaster. BTW, your proposed implementation has the same problem: https://github.com/adams85/filelogger/pull/38/changes#diff-c8fcc66b270a7480057aa3ef1653e60d2c45571d64adee00e307f9f7fcde9354R395-R398 Another option would be to implement a fully synchronous code path. But that would require a lot of code duplication. I need to give this some more thought and weigh up the pros and cons. In the meantime, I'd love to hear your opinion too. |
Introduce SynchronousWrite setting to file logger configuration, allowing log entries to be written directly to file on the calling thread and bypassing the background queue. This ensures logs are persisted before ILogger.Log returns, useful for crash diagnostics. Updated processor, settings, and documentation. Added unit tests for both global and per-file SynchronousWrite behavior.