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; }
}
}
----- Original Message ----
From: "Parrish, Ken" <[EMAIL PROTECTED]>
To: [email protected]
Sent: Thursday, December 13, 2007 10:35:24 AM
Subject: Elapsed time evaluator
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times
New Roman";}
a:link, span.MsoHyperlink
{color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;text-decoration:underline;}
span.EmailStyle17
{font-family:Arial;color:windowtext;}
_filtered {margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{}
-->
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.