tags:

views:

20

answers:

1

Hello there, 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 its 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>
+2  A: 

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.

djechelon
I think thats the problem.. I dont "clean" stop my application (dont know how to?)
ebb
If your application crashes, you are subject to message loss. This is a very common problem in general logging, and because of that you should **always avoid** to buffer messages in a faulty application. By the way, a "clean" stop is a stop without unhandled exception. If you try-catch your main method, it will be a clean stop when you catch an error
djechelon
I am using try-catch. But the problem is that I still want the log to be sent by mail whether its full of errors or just info.
ebb
By setting the buffer size to the maximum possible integer, the following chain of events activates: 1) application terminates main method, 2) .NET performs garbage collection over all the objects to release resources, 3) destructor of SmtpAppender:BufferingAppenderSkeleton:AppenderSkeleton is invoked, 4) an OnClose() method is overriden in SmtpAppender, flushing the buffer to SMTP, 5) application is finally clean-terminated :) [I just read your edit...]
djechelon
Please explain what you mean by **full** of errors or info. You can set filters to log only certain log levels in configuration file, then discard debug for example. Is that what you want to achieve?
djechelon
So setting the buffer size to 2.147.483.647 should do it? - I have already tried with a high buffer size but it seems to stay at "1".. I keep getting a mail for EACH info/warn/fatal/error.info/warn/fatal/errors.
ebb
If I understood your question correctly, then yes. If you want to achieve something else rather than just *having only one aggregate log via email with all the messages generated during runtime* then we might have to consider more steps than this :-)
djechelon
Want I want to achieve is to mail the full log file which include all errors/fatals/infos/warns.
ebb
So.. to send the full log file requires more than just a few attributes?
ebb
From Apache log4net Subversion: (http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/SmtpAppender.cs?view=markup) The number of logging events delivered in this e-mail depend on the value of BufferingAppenderSkeleton.BufferSize option. The SmtpAppender keeps only the last BufferSize logging events in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context. If it doesn't work for you, then there is a *BUG*. Have you spelled BufferSize with correct case in your XML? I have ho other clues right now
djechelon
I'll update the main post with my config file.
ebb
I both tried with BufferSize, bufferSize and Buffersize... no luck.
ebb
Read my update :) I found the solution
djechelon
After I have removed both lossy and evaluator it no longer sends any mail? - The mail server responds, and the BufferSize is equal to 2million.
ebb
My fault, the console application was hanging in a Console.ReadLine(); - Works perfectly now! Thank you very much :)
ebb