http://logging.apache.org/log4net/index.html
Logging to the console in a console app: (thanks to http://www.beefycode.com/?tag=/log4net for the great examples)
static void Main(string[] args)
{
log4net.Config.BasicConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("Hello World!");
log.Info("I'm a simple log4net tutorial.");
log.Warn("... better be careful ...");
log.Error("ruh-roh: an error occurred");
log.Fatal("OMG we're dooooooomed!");
Console.ReadLine();
}
Using app.config
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
notice level is set to INFO only.
Appenders
An object that persists the log messages somewhere.
eg AdoNetAppender, ConsoleAppender, ColoredConsoleAppender, EventLogAppender, FileAppender (don’t use), RollingFileAppender, SmtpAppender
Layout
All the different options:
http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%line %date %-5level %logger - %message%newline" />
</layout>
Real Life
class Program
{
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
Log.Info("this is an info message");
MyClass.WriteLog();
Console.ReadLine();
}
}
class MyClass
{
private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void WriteLog()
{
Log.Info("this is an info message");
}
}
Shows us which class called the logger.
Web Apps
Putting this in AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator()]
Connection Strings and AdoNetAppender
bufferSize value=”1” – otherwise it won’t write each one seperatly
This proved hard
http://weblogs.asp.net/drnetjes/archive/2005/02/16/374780.aspx
doing a search and replace here didn’t work
Nor did extending the provider
http://stackoverflow.com/questions/2441359/can-you-pull-the-connectionstring-for-a-log4net-adonetappender-from-elsewhere-in
** this could be better: http://blog.dynamicprogrammer.com/CommentView,guid,357a3fbe-59e7-4dd4-846a-89083e903532.aspx
Recommended Practice in Code
http://www.beefycode.com/post/Log4Net-Recommended-Practices-pt-1-Your-Code.aspx
Whenever you catch an exception, log it.
Internal Log4Net Logging
When trying to figure out why somethings aren’t working (eg connection strings)
In web.config:
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="c:\\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>