log4net 过滤异常消息?

发布于 2024-08-23 16:02:21 字数 685 浏览 7 评论 0原文

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

代码如下所示:

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>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与清晨阳光 2024-08-30 16:02:21

通过子类化 FilterSkeleton,您可以实现一个过滤器评估异常文本。或者就此而言的异常类型。

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

濫情▎り 2024-08-30 16:02:21

以下是基于 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>

Here are basic implementations based on Peter's 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>
等待我真够勒 2024-08-30 16:02:21

试试这个:

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

编辑:抱歉,没有看到您无法更改该行...

Try this:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文