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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With