如何配置log4net SmtpAppender仅在达到特定级别时才向我发送电子邮件? [英] How do I configure a log4net SmtpAppender to only send me e-mails when a certain level is hit?

查看:315
本文介绍了如何配置log4net SmtpAppender仅在达到特定级别时才向我发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置log4net SmtpAppender,以便仅在达到某个日志级别时才收到一封电子邮件,但包括来自所有级别的最后10行.这是我的配置:

I'm trying to configure a log4net SmtpAppender so that I only get an e-mail if a certain log level is hit, but with the last 10 lines from all levels included. This is my config:

<appender name="EmailAppender" type="SmtpSubjectLayoutAppender">

  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN"/>
  </evaluator>

  <bufferSize value="10" />
  <lossy value="false" />

  ...
</appender>

我正在使用以下代码进行锻炼:

I'm exercising it with this code:

for (var i = 1; i <= 30; i++)
{
    logger.Info("This is just a test message " + i);
}

logger.Error("Error message");

问题是我最终收到3封电子邮件,其中2封邮件记录了所有INFO邮件,而其中一封邮件的最后几行出现在ERROR之前:

The problem is that I end up getting 3 e-mails, 2 with all the INFO logging and one that has the last few lines that occurred before the ERROR:

[2012-07-27 18:59:55.657][INFO ][Chase][tid=14972] This is just a test message 23
[2012-07-27 18:59:55.659][INFO ][Chase][tid=14972] This is just a test message 24
[2012-07-27 18:59:55.661][INFO ][Chase][tid=14972] This is just a test message 25
[2012-07-27 18:59:55.662][INFO ][Chase][tid=14972] This is just a test message 26
[2012-07-27 18:59:55.664][INFO ][Chase][tid=14972] This is just a test message 27
[2012-07-27 18:59:55.666][INFO ][Chase][tid=14972] This is just a test message 28
[2012-07-27 18:59:55.667][INFO ][Chase][tid=14972] This is just a test message 29
[2012-07-27 18:59:55.670][INFO ][Chase][tid=14972] This is just a test message 30
[2012-07-27 18:59:55.671][ERROR][Chase][tid=14972] Error message

如何配置附加程序,以便在发生WARN或更高级别时收到最后10行的电子邮件,否则忽略缓冲区?

How do I configure the appender so that I get an e-mail with the last 10 lines if WARN or higher occurred, but to ignore the buffer otherwise?

推荐答案

您需要将有损值设置为true:

You need to set the lossy value to true:

<lossy value="true" />

在配置中,log4net不仅在记录错误时而且还在缓冲区已满时写入缓冲区.有损标志告诉log4net必要时丢弃消息.

In your configuration log4net writes the buffer not only when an error is logged but also when the buffer is full. The lossy flag tells log4net to discard messages if necessary.

这篇关于如何配置log4net SmtpAppender仅在达到特定级别时才向我发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆