log4net smtpappender 基于日志级别的主题

发布于 2025-01-02 18:09:00 字数 2125 浏览 1 评论 0原文

是否可以将 log4net 配置为具有两个 smtp 附加程序 - 一个仅在日志级别 >= WARN 时发送电子邮件,第二个发送所有内容,但决不能同时发送两个。

<appender name="SmtpAppenderWarn" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]" />
  <from value="[email protected]" />
  <subject value="Import completed with warnings / errors" />
  <smtpHost value="localhost" />
  <bufferSize value="512" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
  </layout>
</appender>
<appender name="SmtpAppenderSuccess" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]" />
  <from value="[email protected]" />
  <subject value="Import completed successfully" />
  <smtpHost value="localhost" />
  <bufferSize value="512" />      
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger: %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="INFO" />
  </filter>      
</appender>

我的应用程序有:

Log.Info("Importer is starting...");
//my logic
//if warn Log.Warn("Warning...");
Log.Info("Import finished");

所以我一直在使用 INFO。现在,当我遇到 WARN 时,两条消息都会被发送。我当然希望没有发送成功信息。

Is it possible to configure log4net to have two smtp appenders - one that sends emails only when log level is >= WARN and second that sends everything, but never two on the same time.

<appender name="SmtpAppenderWarn" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]" />
  <from value="[email protected]" />
  <subject value="Import completed with warnings / errors" />
  <smtpHost value="localhost" />
  <bufferSize value="512" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
  </layout>
</appender>
<appender name="SmtpAppenderSuccess" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]" />
  <from value="[email protected]" />
  <subject value="Import completed successfully" />
  <smtpHost value="localhost" />
  <bufferSize value="512" />      
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger: %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="INFO" />
  </filter>      
</appender>

I my application I have:

Log.Info("Importer is starting...");
//my logic
//if warn Log.Warn("Warning...");
Log.Info("Import finished");

So I'm using INFO all the time. Now when I encounter WARN both messages are being sent. I wish of course that the one with success info was not sent.

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

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

发布评论

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

评论(2

美煞众生 2025-01-09 18:09:00

我已经通过重写 log4net SendBuffer 方法解决了这个问题:

namespace MyProject
{
    public class SmtpAppenderInfoOnly : SmtpAppender
    {
        protected override void SendBuffer(log4net.Core.LoggingEvent[] events)
        {
            foreach (var loggingEvent in events)
            {
                if (loggingEvent.Level != Level.Info)
                    return;
            }

            base.SendBuffer(events);
        }
    }
}

然后在配置文件中:

<appender name="SmtpAppenderSuccess" type="MyProject.SmtpAppenderInfoOnly,MyDll">

这样,当取消 SendBuffer 时,SendMail 方法将不会被执行。

I have solved the problem by overriding log4net SendBuffer method:

namespace MyProject
{
    public class SmtpAppenderInfoOnly : SmtpAppender
    {
        protected override void SendBuffer(log4net.Core.LoggingEvent[] events)
        {
            foreach (var loggingEvent in events)
            {
                if (loggingEvent.Level != Level.Info)
                    return;
            }

            base.SendBuffer(events);
        }
    }
}

And then in config file:

<appender name="SmtpAppenderSuccess" type="MyProject.SmtpAppenderInfoOnly,MyDll">

That way the SendMail method will not be executed when SendBuffer is canceled.

情话墙 2025-01-09 18:09:00

您的附加程序可以具有如下所示的最小/最大值:

<levelMin value="INFO" />
<levelMax value="INFO" />

<levelMin value="WARN" />
<levelMax value="WARN" />

因此您可以针对不同的范围使用不同的附加程序。我相信这应该对你有用。

Your appenders can have min/max values like so:

<levelMin value="INFO" />
<levelMax value="INFO" />

<levelMin value="WARN" />
<levelMax value="WARN" />

So you can use different appenders for different ranges. I believe this should work for you.

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