Use a Timer?

// untested
public class AutoFlushSmtpAppender : SmtpAppender
{
    private System.Timers.Timer timer;

    private int interval;

    /// <summary>
    /// Flush interval in milliseconds.
    /// </summary>
    public int Interval
    {
        get { return interval; }
        set { interval = value; }
    }

    public override void ActivateOptions()
    {
        base.ActivateOptions();
        ErrorHandler = new StopTimerErrorHandler(this, ErrorHandler);

        timer = new Timer(Interval);
        timer.Elapsed += delegate { Flush(); };
        timer.Start();
    }

    protected override void OnClose()
    {
        stopTimer();
        base.OnClose();
    }

    private void stopTimer()
    {
        if (timer.Enabled)
        {
            timer.Enabled = false;
            timer.Stop();
        }
    }

    class StopTimerErrorHandler : IErrorHandler
    {
        private readonly AutoFlushSmtpAppender appender;
        private readonly IErrorHandler wrapped;

        public StopTimerErrorHandler(AutoFlushSmtpAppender appender, 
IErrorHandler wrapped)
        {
            this.appender = appender;
            this.wrapped = wrapped;
        }

        public void Error(string message, Exception e, ErrorCode errorCode)
        {
            appender.stopTimer();
            wrapped.Error(message, e, errorCode);
        }

        public void Error(string message, Exception e)
        {
            appender.stopTimer();
            wrapped.Error(message, e);
        }

        public void Error(string message)
        {
            appender.stopTimer();
            wrapped.Error(message);
        }
    }
}



----- Original Message ----
From: HemantJPatel <[EMAIL PROTECTED]>
To: log4net-dev@logging.apache.org
Sent: Tuesday, September 30, 2008 11:29:53 AM
Subject: Appender to run at a Schedule Time


Hi,

I need to run the smtp appender at a specified interval of time.
Any ideas how I can do this to that the appender will run at specific
intervals and send all the buffered messages.

Any pointers will be helpful.
Thanks in advance.

Thanks,
Hemant
-- 
View this message in context: 
http://www.nabble.com/Appender-to-run-at-a-Schedule-Time-tp19744455p19744455.html
Sent from the Log4net - Dev mailing list archive at Nabble.com.

Reply via email to