Log4net - BufferingForwardingAppender - flush with the timeout

I have log message packages at some points, so I had to post BufferingForwardingAppenderfor performance reasons. But besides these bursts (this happens, say, once a day), during the rest of the day I get a small number of log messages. The problem is that the buffer size is set to 50, which corresponds to the batch update period, but is too strong for periods without a burst. During this period, it may take more than an hour or two to reset the logs, which is not acceptable on this system.

Is there a way to BufferingForwardingAppenderflush at certain intervals (for example, every 10 minutes) if there are not enough messages in the buffer to start a normal process?

+4
source share
2 answers

Guess I'm a little late to help (3 years), but after searching quite a lot, I found something that could help others reach this point. This worked for me:

<appender name="MyBufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender">
      <bufferSize value="1000" /> <!-- flush after 1000 log events -->
      <appender-ref ref="MyRollingFileAppender" />
      <lossy value="false" /> <!-- do not lose any logs -->
      <evaluator type="log4net.Core.TimeEvaluator">
        <interval value="2"/> <!-- flush every two seconds -->
      </evaluator>
</appender>

In the case of OP, it will use <interval value="600"/>to register messages every 10 minutes.

+2
source

Not out of the box, but you can make your own BufferingForwardingAppender based appender :

private static DateTime lastFlushTime = DateTime.Now;
public class TimedBufferingForwardingAppender : BufferingForwardingAppender{
    override protected void Append(LoggingEvent loggingEvent) {
        if (lastFlushTime.AddMinutes(10) < DateTime.Now){
            SendBuffer(new LoggingEvent[] { loggingEvent } );
            lastFlushTime = DateTime.Now;
        }
    }
}
+1
source

All Articles