Tuesday, October 13, 2009

log4net

I keep forgetting how to switch on log4net (using app.config)

I always end up wasting time on the apache docs, and searching the net to find this

Step 0:
Ensure that the first declaration of
"private static readonly log4net.ILog Log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);"
Occurs in your main assembly - if the first declaration is in linked assemblies, it won't load the setting correctly.


Step 1: Edit AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Step 2: Edit App.config
Important! The configSections bit has to be at the top of your config file. So if you've got an existing one, don't just add it on to the end!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

Step 3:

No comments: