Click here to Skip to main content
15,881,863 members
Articles / Programming Languages / C#
Article

log4net NonBufferedSmtpAppenderWithSubjectLayout

Rate me:
Please Sign up or sign in to vote.
4.56/5 (3 votes)
10 Nov 2006Apache1 min read 47.5K   775   21   2
NonBufferedSmtpAppenderWithSubjectLayout is an appender for the log4net logging framework. The mails it sends are non buffered (1 log message = 1 mail) and the subject of the mail can be customized with a layout

Sample Image - outlook.jpg

Introduction

For those who don't know log4net, here is a great article about it. One problem that I have with the SmtpAppender is that it sends a lot of log messages in bulk (it buffers default 250 messages and then sends them in 1 mail). It also doesn't allow you to generate the subject of the mail based on the log message. So I decided to write a modified SmtpAppender that sends it mails non buffered (so 1 log message = 1 mail) and generates the subject based on the log message.

log4net Configuration

Look at the conversion pattern in the subject, it uses a layout to be dynamically generated. <--> The default SmtpAppender's subject is static.

XML
<appender name="smtp" 
  type="HHR.log4net.Appender.NonBufferedSmtpAppenderWithSubjectLayout, 
                                HHR.log4net">
 <to value="myname@mycompany.com" />
 <from value="HHR.log4net.Tests@mycompany.com" />
 <subject type="HHR.log4net.Layout.PatternLayout, HHR.log4net">
  <conversionPattern value="%date %level %exceptType at [%logger] 
                            on %property{log4net:HostName} by %username" />
 </subject>
 <smtpHost value="smtp.mycompany.com" />
 <layout type="log4net.Layout.PatternLayout, log4net">
  <conversionPattern value="%date [%thread] %-5level %logger 
                            [%ndc] &lt;%property{user}&gt; - 
                            %message%newline" />
 </layout>
 <filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="WARN" />
  <levelMax value="FATAL" />
 </filter>   
</appender>

Code

I modified the Subject property from a string to a ILayout.

C#
//I can use a layout for my subject!!!!!!!!!!!!

public ILayout Subject 
{
   get { return m_subject; }
   set { m_subject = value; }
}

I also modified the base class of the Appender from BufferingAppenderSkeleton to AppenderSkeleton. I added an ExceptionTypePatternConverter class that is derived from the PatternLayoutConverter class so that I can use the %exceptType pattern that just returns the class name of the thrown exception.

Conclusion

This looks like a more useful version of the SmtpAppender than the one inside log4net. I included a NUnit test case to play around with. Just modify the 'smtpHost' and the 'to' values in the App.config and off you go.

History

Updated 2006/13/10

  • Added own PatternLayout with %exceptType pattern!
  • Added log4net configuration samples for some other appenders like AdoNetAppender, TraceAppender, ConsoleAppender, RollingFileAppender and EventLogAppender

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer (Senior)
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalfurther customize subject... Pin
Syborg7710-Nov-06 2:21
Syborg7710-Nov-06 2:21 
GeneralRe: further customize subject... [modified] Pin
DaBuddhaMan10-Nov-06 8:53
DaBuddhaMan10-Nov-06 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.