Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4Net + Send Email when done?

Tags:

log4net

I have just started playing around with Log4Net... I now want to send an email with the full log either attached or directly in the mail. The problem with using SmtpAppender is that it requires a bufferSize which will be unknown because it should send the mail whether it's full of errors or just info.

Update: My config file

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">

    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Backup Application - Log" />
    <smtpHost value="mailserver" />
    <authentication value="1" />
    <username value="userName" />
    <password value="mypw" />

    <port value ="25"/>
    <lossy value="true" />
    <bufferSize value="500" />

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

    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%timestamp [%thread] %-5level %logger – %message%newline" />
    </layout>

</appender>
like image 836
ebb Avatar asked Oct 30 '10 23:10

ebb


1 Answers

BufferSize equals to the number of log messages that have to be buffered (ie if you set to 512 the mail will be sent once 512 messages have been collected).

I believe that setting it to int.MaxValue (which is 2.147.483.647) is a reasonable choice. 2 billions of messages are too much for a system, even long-running.

If you give me 10 minutes I'll confirm you (from source code) that if you clean stop your application, all logs collected so far will be sent

[Update]: confirmed!! Destructor flushes the queue as expected

[Add] I would remove both lossy and evaluator. Your problem is clear: Evaluator has precedence over buffer :) :)

The evaluator is used to flush the queue when a certain condition is met. Your condition equals to true. When this condition is triggered, the email is sent, so this is why the mail is sent at every single log call.

This is slightly different from sending ONLY info and error messages, which is achieved by log filtering.

Remove the two attributes and your code will work. Setting int.MaxValue will allow you to store the maximum possible number of messages. It's unlikely (you'd better win the Superenalotto's 178mln€ jackpot like some guys did tonight, or being hit by a comet in your head) that an application collects more than 2 billion errors/info in a run.

like image 54
usr-local-ΕΨΗΕΛΩΝ Avatar answered Oct 25 '22 19:10

usr-local-ΕΨΗΕΛΩΝ