Wednesday, December 17, 2008

Log4Net: Unable to send emails to multiple addresses

The SMTPAppender is supposed to be able to send log information to an email addresses that is specified in the To property.

Here is a basic configuration for the SMTPAppender for the config file.

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
            <to value="toEmailAddresses"/>
            <from value="fromEmailAddresses"/>
            <subject value="subject"/>
            <smtpHost value="smtpHostAddress"/>
            <bufferSize value="1"/>
            <lossy value="true"/>
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
        <!--<threshold value="ERROR"></threshold>-->
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline"/>
      </layout>
</appender>

Based on the SMTPAppender documentation, one should be able to send emails to multiple addresses and this is to be done by using the semi-colon as the delimiter. (documentation)

But if you try and do this, LOG4Net will decide to no longer send an emails. This was perplexing as, when I used a single email address Log4Net would correctly send out an email. But if I tried sending an email to multiple recipients, Log4Net would not send out an email.

After some testing, I found out that the correct delimiter in this case is supposed to be the COMMA (,) and not the semi-colon!

Now you know!

One issue I had while using the Log4Net API was that it seemed to be swallowing up exceptions that were occurring within it. That seemed to be the most logical explanation about what was going on with the multiple email addresses de-limited by semi-colons.

Here is useful information on how you can set Log4Net to emit it's trace information - which can be useful in tracking down these errors.

How do I enable log4net internal debugging?

2 methods are available to turn on internal debugging:

  • via the application's configuration file
    <configuration>....
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    </configuration>
  • set the static log4net.Util.LogLog.InternalDebugging property to true.

Debug messages are written to the console. So for an ASP.net app or a windows forms based app you might think you are out of luck. But here is another little trick.

You can redirect data sent to the console to a file or another application. When the application that is using Log4Net is attached to a debugger, the console information is automatically captured to the output window. But if it is not then you can use a tool like DebugView to capture the output from Log4Net.

To capture Log4Net's trace information to a file you need to add the following information under the configuration node of the application's config file.

<configuration>
    ...
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
    ...
</configuration>

No comments: