2010-02-25 212 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>