简体   繁体   中英

Is there a SeriLog equivalent of log4net's BufferingForwardingAppender for detailed logging only on warning/error?

Is there a way in Serilog to setup a (limited size) log-buffer that collects all log events of a specified level and above (ie DEBUG ) but does not log them into the target sink unless a certain higher level event happens (ie WARNING )?

Basically I'm asking if there is a way to replicate the BufferingForwardingAppender functionality from log4net.

Serilog doesn't have an equivalent of Log4Net's BufferingForwardingAppender out-of-the-box as of this writing, but there are some sinks built by the community that might be useful:

Alternatively it should be easy to create your own Sink to implement that behavior.

In that case you might want to look at the code of Serilog.Sinks.Async for inspiration, as it shows you a way of wrapping multiple sinks into one which would give you the opportunity for buffering the log messages before forwarding to the sinks.

Finally, because you're familiar with Log4Net, you can integrate Log4Net with Serilog and use Log4Net's BufferingForwardingAppender by using the Log4Net Serilog Sink .

A little late I know but I was looking for the same thing today and found Serilog.Sinks.Buffer , which looks to be pretty close to what we want (I haven't actually tried it yet). One limitation, compared to BufferingForwardingAppender, is that, according to the docs ,

The log events that are flushed from a buffer are sent to the output sink only after the triggering event...The result is that the debug log events always end up being sent out of order, ie. after later events and after the triggering event.

This would make it harder to read the log for analysis straight from a text file--you'd need to sort the file by timestamp first to make sense of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM