Flush an ILogger
See original GitHub issueA few questions before you begin:
Does this issue relate to a new feature or an existing bug?
- Bug
- [x ] New Feature
What version of Serilog is affected? Please list the related NuGet package. v2.6.0
What is the target framework and operating system? See target frameworks & net standard matrix.
- [x ] netCore 2.0
- netCore 1.0
- 4.7
- 4.6.x
- 4.5.x
Please describe the current behavior? Any pending writes can only be flushed by effectively disposing the entire log pipeline (usually at the end of the app).
Please describe the expected behavior? The ability to Flush pending writes in the sink pipeline and receive a notification (preferable using an Async pattern) once all pending writes have been written.
There are points in a program logic where it is desirable to know that log events prior to that point have been committed before proceeding. This would involve doing a Flush of some sort and waiting for the Flush to complete. Currently CloseAndFlush() or Dispose can only really be called at the end of the application because both effectively tear down the existing logging pipeline. It would be great if there was a way to Flush at an intermediary point.
Two suggestions on how one might think about this:
- Add a
Flushmethod toILogger. This would flush the pipeline and resolve when all pending writes are written. Since of course new writes may be introduced concurrently, the Flush represents a partial order for concurrent events, but should obey temporal causality for events written before the flush. - Make
ILoggerIDisposable(instead of justLogger). Then sub-loggers could be created which when disposed would imply that all events written from thatILoggerinstances have been flushed. This provides a weaker order guarantee since it doesn’t say anything about log events that were written by otherILoggerinstances before the dispose event, but lends itself to hierarchical structuring better. (Of course this could still be achieved withtry {...} finally { logger.Flush()}for case (1).
Since Flush necessitates blocking for I/O (either disk or network or both) it seems reasonable that this should implement some kind of async pattern (while individual log calls are expected to be buffered and likely return immediately). Ideally this would use Task (or ValueTask now that it is available generally), but since Serilog doesn’t currently incorporate any use of Task there might be better choices.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:22 (11 by maintainers)
Top Related StackOverflow Question
Nicholas,
I can share our use case where Flush method will be very usefull:
In our case we it will be ok if some sinks drop events during flush in case of some kind failures.
Thank you!
CloseAndFlush()works just fine for the ASP.NET Core app I’m working on. I’ve found, however, that I have to Sleep(2000) or so before the app terminates so that all the sinks can do their business.`