Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net - BufferingForwardingAppender - flush with timeout

I have a bursts of log messages at some points, so I had to put BufferingForwardingAppender for performance reasons. But, aside from this bursts (that happen lets say, once a day), during the rest of day, I get a small amount of log messages. Problem is that buffer size is set to 50, which is OK for the burst period, but that's way too much for the periods without burst. In this period, it may take more than an hour or two to flush logs, which is unacceptable in this system.

Is there some way to make BufferingForwardingAppender flush in specific time intervals (eg. every 10 minutes) if not enough messages are in buffer to trigger the usual process?

like image 857
bkovacic Avatar asked Oct 11 '13 09:10

bkovacic


2 Answers

Guess I'm kinda late to help (3 years), but after searching quite a bit, I found something that might help others reaching this question. 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 OP case, he would use <interval value="600"/> to log messages every 10 minutes.

like image 109
André Mantas Avatar answered Oct 11 '22 19:10

André Mantas


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

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;
        }
    }
}
like image 38
Peter Avatar answered Oct 11 '22 18:10

Peter