The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.
| Author |
Messages |
|
Parrish, Ken
 |
| 12/13/2007 4:35 PM |
|
As a follow up to my question about metering SMTP messages, I was thinking about the possibility of creating a custom Evaluator that works on elapsed time. Whereas the LevelEvaluator triggers on a particular level of message, this evaluator would trigger on a specified elapsed time.
In the case of and SMTP appender, an ‘ElapsedTimeEvaluator’ would wait for a specified period of time before sending an e-mail with all the messages in it’s buffer.
Has anyone attempted to create a custom evaluator? Has anyone ever see a similar custom implementation for log4net? Ideas, comments?
Thanks,
Ken Parrish
Gomez, Inc. |
|
|
|
|
Ron Grabowski
 |
| 12/14/2007 6:38 AM |
|
This might work:
// untested public class IntervalEvalulator : ITriggeringEventEvaluator { private DateTime lastTriggeringEvent = DateTime.MinValue; private int intervalSeconds = 60;
public bool IsTriggeringEvent(LoggingEvent loggingEvent) { if (lastTriggeringEvent == DateTime.MinValue) { lastTriggeringEvent = loggingEvent.TimeStamp; return false; } else { TimeSpan diff = loggingEvent.TimeStamp - lastTriggeringEvent; lastTriggeringEvent = loggingEvent.TimeStamp; return (diff.TotalSeconds > intervalSeconds); } }
public int IntervalSeconds { get { return intervalSeconds; } set { intervalSeconds = value; } } }
You could also create an appender that flushes at a predefined interval:
// untested public class FlushIntervalSmtpAppender : SmtpAppender { private Timer timer; private int intervalSeconds;
public override void ActivateOptions() { if (IntervalSeconds > 0) { timer = new Timer(IntervalSeconds); timer.Elapsed += delegate { if (BufferSize > 0) Flush(true); }; LogManager.GetRepository().ShutdownEvent += delegate { timer.Stop(); }; timer.Start(); }
base.ActivateOptions(); }
public double IntervalSeconds { get { return intervalSeconds; } set { intervalSeconds = value; } } }
|
|
|
|
|
Parrish, Ken
 |
| 12/17/2007 4:04 PM |
|
Ron,
Thank you so much for the help. This looks like it will do the job. I will be a few days before I can work this up, but will let you know how it goes.
Thanks,
Ken Parrish
Gomez, Inc.
|
|
|
|
|
|
| You are not authorized to post a reply. |
|
|
|
ActiveForums 3.7
|