0

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.

stuartd
  • 70,509
  • 14
  • 132
  • 163
DougJones
  • 774
  • 1
  • 9
  • 26
  • Why don't you se the builtin [ForwardingAppender](https://logging.apache.org/log4net/release/sdk/log4net.Appender.ForwardingAppender.html)? It it because you want the `DbBufferedAppender` to be static? Is there a particular reason for that? – stuartd Oct 07 '15 at 13:38
  • @stuartd Yes, I want the `DbBufferedAppender` to be static. The reason is so that on an application level I can log once every x log event messages come through regardless of which logger logs it and which associated instance of an appender logs it. Otherwise I'm logging per logger and some of our loggers aren't static and are very short lived. – DougJones Oct 08 '15 at 14:31

0 Answers0