log4net smtpappender 基于日志级别的主题
是否可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经通过重写 log4net SendBuffer 方法解决了这个问题:
然后在配置文件中:
这样,当取消 SendBuffer 时,SendMail 方法将不会被执行。
I have solved the problem by overriding log4net SendBuffer method:
And then in config file:
That way the SendMail method will not be executed when SendBuffer is canceled.
您的附加程序可以具有如下所示的最小/最大值:
因此您可以针对不同的范围使用不同的附加程序。我相信这应该对你有用。
Your appenders can have min/max values like so:
So you can use different appenders for different ranges. I believe this should work for you.