log4net过滤异常消息?

时间:2010-02-25 14:56:52

标签: exception filter log4net

如何根据记录的异常消息过滤日志记录?

代码如下所示:

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

Config看起来像这样:

<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>

我发现我只能过滤掉已记录的消息(“嘿,我有一个错误”),但它似乎忽略了异常的消息。由于这是在我们的生产环境中,我不能进行任何代码更改,因此我无法更改记录的消息。是否有一些配置指定也检查异常的消息?

3 个答案:

答案 0 :(得分:2)

通过子类化FilterSkeleton,您可以实现一个评估异常文本的过滤器。或者那个例外类型。

答案 1 :(得分:0)

试试这个:

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

编辑:对不起,没看到你无法改变那条线......

答案 2 :(得分:0)

以下是基于Peter接受的答案的基本实现

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;
        }
    }
}

配置文件

<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>