Hi,
I was trying to download the log4net zip file and trying to
extract it is giving me error, can anyone help me in finding the working code.
Thanks
Srini
From: Parrish, Ken
[mailto:KParrish <at> gomez.com]
Sent: Monday, February 04, 2008 11:10 AM
To: log4net-user <at> logging.apache.org
Subject: A caching / metering SMTP appender ...
Several
weeks ago, I posted a couple of notes on this list on the subject of
metering of SMTP log events and an elapsed time evaluator. The objective
was to design a log4net extension that would accommodate the caching and
metering of log events sent via SMTP. In a situation where a server is
failing and generating large quantities of severe log events that need to be
sent via SMTP to notify operational personnel, the problem was to find a way to
collect blocks of messages and send them in a single e-mail message.
Since then, I’ve notice several other posting referring to similar problems.
Ron
Grabowski sent me some snippets of code suggesting possible approaches to the
elapsed timer portion of the problem which got me started on creating a more
comprehensive solution to this problem.
Below
/ attached is the source code and sample configuration for a caching SMTP
appender. There are two configurable criteria for this appender:
FlushInterval TimeSpan indicating
maximum elapsed time between generation of e-mail messages. Needed in
cases where there is a quiescent period of time after the caching of critical
errors.
MaxCachedMessages Integer indicate the maximum number of
log messages to cache before flushing to e-mail. This total does not
include ‘context’ log events that are normally and optionally included in SMTP
log event postings.
I
wanted to share this with other log4net users, but also ask for feedback
regarding it’s design. Have I overlooked any operational issues, resource
management, etc. Is there a way to gracefully shutdown the time when
log4net is shutdown? Any other comments or feedback would be appreciated.
Thanks,
Ken
Parrish
<appender name="SmtpCachingAppender" type="Log4NetExtensions.SmtpCachingAppender, Log4NetExtensions">
<to value="name <at> domain.com" />
<from value="name <at> domain.com" />
<subject value="Log4net test message"
/>
<smtpHost value="smtphost.com" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy-MM-dd HH:mm:ss.fff} [%t] %-5level %logger -
%message%newline%exception" />
</layout>
<FlushInterval value="00:05:00" />
<MaxCachedMessages value="20" />
</appender>
using System;
using System.Collections.Generic;
using log4net.Appender;
using log4net.Core;
namespace Log4NetExtensions
{
public class SmtpCachingAppender : SmtpAppender
{
int _maxCachedMessages = 20; // Config option
TimeSpan _flushInterval = new TimeSpan(0, 5,
00); // Config option
System.Threading.Timer _timer;
bool _timedFlush = false;
int _numCachedMessages = 0;
List<LoggingEvent>
_loggingEvents = new List<LoggingEvent>();
/// <summary>
///
TimeSpan indicating the maximum period of time elapsed before sending
///
any cached SMTP log events.
/// </summary>
public TimeSpan
FlushInterval
{
get { return this._flushInterval; }
set { this._flushInterval
= value; }
}
/// <summary>
///
Maximium number of SMTP events to cache before sending via SMTP.
/// </summary>
public int
MaxCachedMessages
{
get { return this._maxCachedMessages; }
set { this._maxCachedMessages
= value; }
}
/// <summary>
///
Create a timer that fires to force flushing cached log events
///
via SMTP at a specified interval.
/// </summary>
public override
void ActivateOptions()
{
this._timer = new
System.Threading.Timer(
new System.Threading.TimerCallback(OnTimer),
null,
TimeSpan.Zero,
this._flushInterval);
base.ActivateOptions();
}
void OnTimer(Object
stateInfo)
{
this._timedFlush = true;
Flush(true);
}
protected override
void SendBuffer(LoggingEvent[]
events)
{
foreach (LoggingEvent
loggingEvent in events)
{
this._loggingEvents.Add(loggingEvent);
}
this._numCachedMessages++;
if ((this._numCachedMessages
>= this._maxCachedMessages) || this._timedFlush)
{
if (this._loggingEvents.Count
> 0)
{
LoggingEvent[]
bufferedEvents = this._loggingEvents.ToArray();
base.SendBuffer(bufferedEvents);
this._loggingEvents.Clear();
}
// Reset cache buffer conditions.
this._numCachedMessages = 0;
this._timedFlush = false;
}
}
}
}