I'd like a static buffered appender for my application. I found a way to accomplish this, but it's a bit of a hack. As shown below, it has a buffer of 3. When that buffer fills, the SendBuffer(LoggingEvent[] events) method gets called. This should be application wide. If any logger gets a message it should forward it to this static buffered appender, which will write out 4 messages at once to a db when it has them.
My implementation (hack) is a new ForwardingDbBufferedAppender (BufferingAppenderSkeleton) that forwards the messages as it gets them to a static DbBufferedAppender (also new BufferingAppenderSkeleton), shown here:
public class ForwardingDbBufferedAppender : log4net.Appender.BufferingAppenderSkeleton
{
private static readonly object m_lockObj = new object();
private static DbBufferedAppender m_dbBufferedAppender;
public override void ActivateOptions()
{
lock (m_lockObj)
{
if (m_dbBufferedAppender == null)
{
m_dbBufferedAppender = new DbBufferedAppender(this.BufferSize);
}
}
base.ActivateOptions();
}
protected override void Append(LoggingEvent loggingEvent)
{
m_dbBufferedAppender.DoAppend(loggingEvent);
}
protected override void SendBuffer(LoggingEvent[] events)
{
}
Config:
<log4net>
<appender name="ForwardingDbBufferedAppender" type="Blah.ForwardingDbBufferedAppender, Blah">
<bufferSize value="3" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ForwardingDbBufferedAppender" />
</root>
</log4net>
Is there a better way? Right now this is a total hack because I'm only forwarding the BufferSize. What if I add a range filter or switch lossy to true and add an evaluator, etc... Right now I'd have to forward all the config possibilities to the static DbBufferedAppender.