logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Suppress repeated, identical log entries and add a summary log entry
Date Sat, 04 Aug 2007 04:15:49 GMT
Filters can't create new log messages because of a recursive guard in AppenderSkeleton's DoAppend
method. 

Suppose have a filter called ToUpperRepeaterFilter that creates a new logging event from the
existing event but modifies its message to be in upper-case (similiar to how your filter may
generate a new summary message once the duplicates have stopped):

// DOESN'T WORK!
public class ToUpperRepeaterFilter : FilterSkeleton
{
    private IAppender appender = null;

    public override FilterDecision Decide(LoggingEvent loggingEvent)
    {
        LoggingEventData loggingEventData = loggingEvent.GetLoggingEventData();
        loggingEventData.Message = loggingEventData.Message.ToUpper();

        // DOESN'T WORK - recursive guard exits DoAppend early
        Appender.DoAppend(new LoggingEvent(loggingEventData));
        
        return FilterDecision.Accept;
    }

    public IAppender Appender
    {
        get { return this.appender; }
        set { this.appender = value; }
    }
}

Its important to note that Filters don't keep track of the Appender they are associated with.
I had to write that code on my own:

foreach (AppenderSkeleton appenderSkeleton in LogManager.GetRepository().GetAppenders())
{
    IFilter filter = appenderSkeleton.FilterHead;
    while (filter != null)
    {
        ToUpperRepeaterFilter toUpperRepeaterFilter = filter as ToUpperRepeaterFilter;
        if (toUpperRepeaterFilter != null)
        {
            toUpperRepeaterFilter.Appender = appenderSkeleton;
        }

        filter = filter.Next;
    }
}

You may be better off using one of the buffering appenders. I recall posting a sample Filter
class that recognized duplicate messages and removed them. I don't think it created a summary
message though.

----- Original Message ----
From: "Giovannini, Stephanie " <Stephanie.Giovannini@anheuser-busch.com>
To: log4net-user@logging.apache.org
Sent: Friday, August 3, 2007 4:24:17 PM
Subject: Suppress repeated, identical log entries and add a summary log entry

I'm trying to design a custom filter using log4net. I'm in the conceptual stage
of design and I don't see how I can implement the entire design.

The problem is that I want a part of log4net, a filter, to be able to generate
logging entries.

I want to make a filter that will suppress identical log messages from the same
logger if they occur too soon after each other, and then emit a log entry when
the error condition stops. I'll illustrate with an example.

An external device is polled once a second. The device begins to return errors.
The logger calls the Error method. The first error is logged as usual. The
device then returns the exact same error once a second for 10 minutes. The
filter denies all of those entries because they are identical to a log entry
that was logged within a specific expiration period (configurable aspect of
filter, in this example, let's say 2 minutes).

I think I can do what's above. The kicker is the final part, illustrated below.

One expiration period (2 minutes in this example) after the last identical log
entry was denied by the filter, the filter generates a log entry stating that
the error message X had occurred Y times and the last occurrence was at Z time.
This entry should be logged from the same logger that made all the identical log
entries.

I haven't written any code for this because I want to know that it's feasible.
I'm not sure how to get the class that implements IFilter to generate a log
entry from the same logger that submitted the original entries.

Any ideas?

Thanks for reading this...

The information transmitted (including attachments) is
covered by the Electronic Communications Privacy Act,
18 U.S.C. 2510-2521, is intended only for the person(s) or
entity/entities to which it is addressed and may contain
confidential and/or privileged material. Any review,
retransmission, dissemination or other use of, or taking
of any action in reliance upon, this information by persons
or entities other than the intended recipient(s) is prohibited.
If you received this in error, please contact the sender and
delete the material from any computer.






Mime
View raw message