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

本文是小编为大家收集整理的关于我如何配置log4net SmtpAppender,使其只在达到某个级别时向我发送电子邮件?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试配置log4net smtpappender,以便我只有在点击某个日志级别时才获得电子邮件,但其中包含所有级别的最后10行.这是我的配置:

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

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

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

  ...
</appender>

我正在使用此代码进行练习:

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

logger.Error("Error message");

问题是,我最终收到了3封电子邮件,2张INFO记录,还有一条具有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

我如何配置附录器,以便如果发生警告或更高的话,我会收到带有最后10行的电子邮件,但否则忽略了缓冲区?

推荐答案

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

<lossy value="true" />

在您的配置中,log4net不仅在记录错误时,而且在缓冲区已满时都会编写缓冲区.有损失的标志告诉Log4net,如有必要.

其他推荐答案

使用

<threshold value="WARN"/>

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

似乎不起作用(log4net版本1.2.13.0)...

使用

<lossy value="true" />

当一个人想要获取消息时不好.

其他推荐答案

我会尝试一下:

在log4net?

过滤在其他"方案"中对我很好.

本文地址:https://www.itbaoku.cn/post/1574929.html

问题描述

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");

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

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?

推荐答案

You need to set the lossy value to true:

<lossy value="true" />

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.

其他推荐答案

Use

<threshold value="WARN"/>

The

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

seems not to work (log4net version 1.2.13.0) anymore...

Using

<lossy value="true" />

is not good when one does want to get the messages.

其他推荐答案

I'd give this a try:

How do I Filter on a custom Level in log4net?

Filtering works nicely for me in other 'scenarios'.