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:
- 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.
- 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>