Thursday, August 30, 2007

Using Log4Net in 4 Simple Steps

First off, yes, this is a complete rip-off from this blog post, but with value added in that this represents my personal preference when using log4net. This is a simple four step process of adding logging to an application. I prefer log4net over the MS logging application block, primarily as log4net has zero dependencies and NHibernate already references it. IMHO, if you're doing ANY kind of logging, reference log4net into your project and use it instead of Trace.WriteLine() or Debug.WriteLine().

Step 1. Add the following in the AssemblyInfo.cs

[assembly: log4net.Config.XmlConfiguratorAttribute(
ConfigFile = "log4net.xml", Watch = true)]
Step 2. Create the log4net.xml file and add the following to the new file

With respect to the location of this file and SharePoint, this file can exist in the root of the wss root, next to the relevant web.config file for the given site. In general, this should sit next to the app.config or web.config file for the given application.

<log4net>
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\\Logs\\AppName_${COMPUTERNAME} " />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value=".yyyyMMdd.'log'" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="GeneralLog" />
<appender-ref ref="DebugAppender" />
</root>
<!-- Print only messages of level ERROR or above in the package NHibernate -->
<logger name="NHibernate" additivity="true">
<level value="ERROR" />
</logger>
</log4net>

Step 3. Add the following at the top of your class
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Step 4. Add the following line in the code where you want logging to be done

log.Info("Program Started " + DateTime.Now.ToString ());
References

Apache log4net Home Page - http://logging.apache.org/log4net/

6 comments:

Anonymous said...

Simple! Clear! Working! This is what I am looking for. Good job!

Anonymous said...

step 4 is better implemented as
if (log.IsInfoEnabled) log.Info("Program Started " + DateTime.Now.ToString ());
to reduce the impact of the string formatting where the log isn't enabled

mnrp said...

Re last comment: If you want to make your (not) logging code as fast as possible, see "What is REALLY the FASTEST way of (not) logging?" in the FAQ section of documentation. Anyway, thanks for the post BigJim.

BigJimInDC said...

Just as a quick follow-up to this, if you're like me, and prefer to use a separate log4net configuration file, rather than storing the configuration info inside the app.config or web.config file, AND if you followed my example to a tee, you created a file named "log4net.xml".

For the record, I would advise changing that to "log4net.config", especially if you are running this on a web server, as the typical IIS/ASP.NET configuration is to NOT serve up ".config" files. That will plug a potential security hole.

Paul Wheeler said...

If you put the configuration from Step2 into the web.config like I do, you need to put a call to XmlConfigurator.Configure() into your Global Application_Start method:
log4net.Config.XmlConfigurator.Configure();

Anonymous said...

Doesn't work.