You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Barvaz Barvaz <ba...@gmail.com> on 2009/12/17 17:10:28 UTC

configuration for timout/buffer forward appender

Hi,

I'd like to know if it's possible to configure an appender (or a forward
appender) that can do the following:
1. Buffer all incoming log messages, in a loss-less manner
2. Flush the buffer when it reaches N messages, OR when a timeout of T
seconds reached since the first message (since app start or last flush)
3. Flush the buffer if the application is terminated.

Do you think this is achievable?

Thanks,

-BM

Re: configuration for timout/buffer forward appender

Posted by Loren Keagle <Lo...@aps-technology.com>.
On 12/17/2009 8:10 AM, Barvaz Barvaz wrote:
> Hi,
>
> I'd like to know if it's possible to configure an appender (or a 
> forward appender) that can do the following:
> 1. Buffer all incoming log messages, in a loss-less manner
> 2. Flush the buffer when it reaches N messages, OR when a timeout of T 
> seconds reached since the first message (since app start or last flush)
> 3. Flush the buffer if the application is terminated.
>
> Do you think this is achievable?
>
> Thanks,
>
> -BM
>

It's easily done.  I had to do this for a Remoting appender, but you 
could easily adapt it to an buffered appender, or maybe add it to the 
base class.  You can us a different timer, or even a thread if you have 
limitations on using the thread pool.

~Loren

public class TimedRemotingAppender : RemotingAppender
     {
         public TimedRemotingAppender()
         {
             FlushPeriod = 10;
             timer = new Timer(FlushBuffer);
         }

         /// <summary>
         /// The period, in seconds, at which the buffer is sent 
regardless of being full
         /// </summary>
         public int FlushPeriod { get; set; }

         private readonly Timer timer;
         private void FlushBuffer(object state) { Flush(); }

         protected override void Append(LoggingEvent loggingEvent)
         {
             base.Append(loggingEvent);
             timer.Change(FlushPeriod * 1000, Timeout.Infinite);
         }
     }