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?
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.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With