Monday, October 27, 2008

Log4net .NET Logging

Log4net makes logging in .NET easy. For myself I can say that using logging makes me a far more effective developer. Plus you can turn up interesting stuff :P

3 steps to get log4net

1.) Add the log4net.dll assembly to your project
2.) Modify a *.config file to include an appender
3.) Add code to load the config

1.) Is self explanatory



2.)

<?xml version="1.0"?><configuration>
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<appSettings> <add key="log4net.Internal.Debug" value="true" /> </appSettings>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" /> <appender-ref ref="ConsoleAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="logs/log2.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>



3.) In assembly.info add
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
or
Log4net.Config.XmlConfigurator.Configure()

No comments: