2010-02-25 155 views
3

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

代码如下所示:

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

配置是这样的:

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

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

回答

2

通过继承FilterSkeleton,你可以实现一个过滤器,用于评估异常文本。或者这个问题的异常类型。

+0

这是有道理的基本实现。 – 2010-02-25 16:23:17

0

试试这个:

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

编辑:对不起,没有看到,你不能改变这条线......

0

下面是基于彼得接受的答案

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>