Filtering log4net to exception message?

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

The code is as follows:

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

The configuration 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 find that I can only filter on a registered message ("Hi, I have an error"), but it seems to ignore the exception message. Since this is in our production environment, I cannot change any code changes, so I cannot change the registered message. Is there some kind of configuration that will also point to checking the exception message?

+3
source share
3 answers
+2

:

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

: , , ...

0

Here are the main implementations based on Peter accepted answer

using System;
using log4net.Core;

namespace log4net.Filter
{
    public abstract class ExceptionFilterBase : FilterSkeleton
    {
        public override FilterDecision Decide(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
                throw new ArgumentNullException("loggingEvent");

            var str = GetString(loggingEvent);
            if (StringToMatch == null || string.IsNullOrEmpty(str) || !str.Contains(StringToMatch))
                return FilterDecision.Neutral;
            return AcceptOnMatch ? FilterDecision.Accept : FilterDecision.Deny;
        }

        protected abstract string GetString(LoggingEvent loggingEvent);

        public string StringToMatch { get; set; }

        public bool AcceptOnMatch { get; set; }
    }

    public class ExceptionMessageFilter : ExceptionFilterBase
    {
        protected override string GetString(LoggingEvent loggingEvent)
        {
            return loggingEvent.ExceptionObject == null
                ? null : loggingEvent.ExceptionObject.Message;
        }
    }

    public class ExceptionTypeFilter : ExceptionFilterBase
    {
        protected override string GetString(LoggingEvent loggingEvent)
        {
            return loggingEvent.ExceptionObject == null
                ? null : loggingEvent.ExceptionObject.GetType().FullName;
        }
    }

    public class ExceptionStackFilter : ExceptionFilterBase
    {
        protected override string GetString(LoggingEvent loggingEvent)
        {
            return loggingEvent.ExceptionObject == null
                ? null : loggingEvent.ExceptionObject.StackTrace;
        }
    }
}

configuration file

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="Client.log" />
  <layout type="log4net.Layout.PatternLayout">
   <conversionPattern value="%date{yyyy/MM/dd HH:mm:ss,fff} [%-5level] %logger - %message%newline" />
  </layout>
  <filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="Token is not valid." />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.ExceptionMessageFilter, YourAssembly">
    <stringToMatch value="Application is not installed." />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.ExceptionTypeFilter, YourAssembly">
    <stringToMatch value="System.Deployment.Application.DeploymentException" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.ExceptionStackFilter, YourAssembly">
    <stringToMatch value="at System.Deployment.Application.ComponentStore.GetPropertyString(DefinitionAppId appId, String propName)" />
    <acceptOnMatch value="false" />
  </filter>
</appender>
0
source

Source: https://habr.com/ru/post/1734424/


All Articles