Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 910
Daniel T. Avatar asked Jul 28 '12 05:07

Daniel T.


2 Answers

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.

like image 114
Stefan Egli Avatar answered Oct 27 '22 10:10

Stefan Egli


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.

like image 23
Motlicek Petr Avatar answered Oct 27 '22 11:10

Motlicek Petr