March 2007 - Posts

A realistic log4net config

Most log4net config file examples show the simplest case.  Here is a more realistic example of a production log4net config that uses multiple appenders.  This comes from an ASP.NET application, but the same technique will work in a server or client application.  This config sets up two appenders:

  1. The first one writes all messages at DEBUG or higher to a log file.  Depending on your needs, a RollingFileAppender that creates a new file every day or week might be more appropriate.
  2. The second appender sends email messages when a new user account is created, or when an error is logged.

<?xml version="1.0"?>

<configuration>

<configSections>

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

</configSections>

<log4net>

<!-- The DebugFileAppender writes all messages to a log file-->

<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">

<file value="LinkedCellsWebService.log" />

<threshold value="DEBUG" />

<appendToFile value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%5p [%d] - %m%n" />

</layout>

</appender>

<!-- The EmailAppender sends an email when something matches the filters-->

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

<evaluator type="log4net.Core.LevelEvaluator">

<threshold value="DEBUG"/>

</evaluator>

<!-- The filters are processed in order:

1) match the Inserted New User message

2) match any WARN or higher messages

3) reject everything else -->

<filter type="log4net.Filter.StringMatchFilter">

<stringToMatch value="Inserted a new user" />

<acceptOnMatch value="true" />

</filter>

<filter type="log4net.Filter.LevelRangeFilter">

<levelMin value="WARN" />

<acceptOnMatch value="true" />

</filter>

<filter type="log4net.Filter.DenyAllFilter" />

<!-- The SmtpAppender authenticates against the mail server, the buffersize of 10 provides 10 lines

of context when an error happens. -->

<subject value="LinkedCells: Production log event" />

<to value="notifications@LinkedCells.com" />

<from value="notifications@LinkedCells.com" />

<password value ="password" />

<smtpHost value="MAILSERVER" />

<bufferSize value="10" />

<lossy value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="ConversionPattern" value="%5p [%d] - %m%n" />

</layout>

</appender>

<root>

<!-- add other appenders here and the log messages will be sent to every listed appender -->

<appender-ref ref="DebugFileAppender" />

<appender-ref ref="EmailAppender" />

</root>

</log4net>

----- END OF CONFIG -----
Software Startup Series
My series highlighting helpful information for software startups is now in the 8th week.  I started this as a way to communicate helpful information with my partners, but it is starting to get some traffic and take on a life of its own.  The micro-ISV movement is gaining momentum, Channel 9 has even started a show about it.
More Posts