Logging can be a very useful tool when developing any application. Furthermore, logging is a must when deploying applications to a production environment. Keeping track of errors and informational messages can help you greatly reduce the time it takes to debug and fix a problem. As in any language, there is more than one way to enable logging in .NET. This article will cover getting started with logging using the free log4net [1] framework.
Configuration
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="sampleLog.log" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
private static String m_log4netConfigFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "log4net.config");
private static readonly ILog m_log = LogManager.GetLogger(typeof(Form1));
xcopy /y "$(ProjectDir)log4net.config" "$(TargetDir)"
m_log.Debug("Inside Form1_Load");
I have covered the basics of getting started. However, log4net has quite a few more possible configurations such as
More information for each configuration can be found at http://logging.apache.org/log4net/release/config-examples.html [3]
Links:
[1] http://logging.apache.org/log4net/
[2] http://logging.apache.org/log4net/download.html
[3] http://logging.apache.org/log4net/release/config-examples.html
[4] http://tech-cats.net/blog/examples/dotnet/sampleLoggingApp.zip
[5] http://blog.tech-cats.com/2008/03/simple-logging-in-your-net-application.html