How do I configure a log4net SmtpAppender to only send me e-mails when a certain level is hit?

℡╲_俬逩灬. 提交于 2019-12-07 05:46:14

问题


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?


回答1:


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.




回答2:


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.




回答3:


I'd give this a try:

How do I Filter on a custom Level in log4net?

Filtering works nicely for me in other 'scenarios'.



来源:https://stackoverflow.com/questions/11698452/how-do-i-configure-a-log4net-smtpappender-to-only-send-me-e-mails-when-a-certain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!