views:

116

answers:

2

How can I filter logging based on a logged exception's message?

Code looks like this:

try 
{ 
    someService.DoSomeWorkflow(); 
}
catch(Exception e)
{
    log.Error("Hey I have an error", e);
}

Config looks like this:

<appender name="EventLogger" type="log4net.Appender.EventLogAppender">
    <applicationName value="foo" />
    <layout type="log4net.Layout.PatternLayout" value="PID:%P{pid}: %message" />
    <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="TextInsideTheException" />
    </filter>
</appender>

I'm finding that I can filter only on the logged message ("Hey I have an error") but it seemingly ignores the exception's message. Since this is in our production environment I can't make any code changes so I can't change the logged message. Is there some configuration that would specify to also check the exception's message?

A: 

Try this:

log.Error("Hey I have an error: " + e.Message);

Edit: Sorry, didn't see that you cannot change that line...

Simon Linder
+1  A: 

By subclassing FilterSkeleton, you can implement a filter that evaluates the exception text. Or exception type for that matter.

Peter Lillevold
That makes sense.
Nick Swarr