Enterprise Library Logging vs Log4Net for a rolling file log : jumpstart and a showdown!

I was looking for a rolling file log and couldn't make my mind between the two. So I embarked on a small quest to see which one is easier to learn and work with.


First up I'm gonna create a VS2010 Console application.

Enterprise Library
Ok first up we need to download the latest drop of EnterpriseLibrary which is currently 5.0. Run through the install.
Strafe the web a bit. Okay.. we need an app.config file in our project with some XML muck. Thankfully MS has a GUI tool to help us write it - it's named EntLibConfig.NET4.exe and lies in the EntLib install folder. Open it up and we can either create/load an app.config file.

  1. Main Menu > Blocks > Add Logging Block
  2. An Event logging Listener is added by default (2nd column). Delete it and add a Rolling Flat File Trace Listener - click the Plus sign next to 'Logging Target Listeners' caption.
  3. Next go on a property-setting spree. It should end up like this

We'll work our way right to left.
Third Column : TextFormatter.
  • Template = {timestamp(local)} : {severity} - {message}.
The ellipses button opens up a editor which has a dropdown with all the special placeholder incantations to insert all sorts of informative bits into your log message. The default template by the way is pretty detailed for a default.

Second Column : Rolling flat file trace listener
  • File Name -> rolling-with-entlib.log
  • Formatter - choose TextFormatter (which is present by default. Else add one in the third column via the context menu.) and you should see a link between the trace listener and the formatter.
  • Max Archive Files - set to 2. Older messages will be sent to an archive file.
  • Message Footer, Header - clear out.
  • Roll Interval -> None
  • Roll Size KB -> 200
First column : Logging category.
By default, a 'General' logging category is added. I'll stick to that for simplicity (but you could add more of your own). Configure the Listeners property of the category to point to the rolling flat file listener. You should see another link.
We need one more link (the last I promise) to make the config valid. Open up the 'Logging errors & warnings' and make it point to the same listener. Another link appears and we're done. Save the file.

Jump back into the IDE. Add a reference to the Microsoft.Practices.EnterpriseLibrary.Logging from the .NET tab. Write ourselves a new PSVM method with

Logger.Write("Dilshan flings himself for the catch to send Kohli back!", "General", 0, 0, TraceEventType.Critical);
Logger.Write("MS Dhoni walks in to steady the innings.", "General", 0, 0, TraceEventType.Information);

Log4Net
Looks like we need the binaries and ... yup.. some more XML. But there's no GUI to help us this time around. However there is a kind soul on the internet writing tutorials at BeefyCode.com. Two thumbs way up! By Part 5 I think I know all I need... for now.

   <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level- %message%newline"/>
</layout>
<file value="rolling-with-log4net.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="1" />
<maximumFileSize value="200KB" />
<staticLogFileName value="true" />
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>

</log4net>
A quick summary: Log4Net has appenders (instead of EntLib's trace listeners) whose properties need to be set. A root node controls the level of logging + a reference to the appender to use. The PatternLayout node controls how the message is logged (akin to EntLib's formatter template property) with the magic placeholders. That's it.

Back to the IDE. Add a reference to log4net.dll.

log4net.Config.XmlConfigurator.Configure();
var logger = log4net.LogManager.GetLogger(typeof(Program)); // Program is the name of my entry point class

logger.Error("Dilshan flings himself for the catch to send Kohli back!");
logger.Info("MS Dhoni walks in to steady the innings.");
That's all there is to it. I had to change the project's target framework to ".Net Fwk 4" for the build to succeed. The client profile doesn't have System.Web that log4net needs for some reason.

Here's how it looks in the log files (I added a running counter into the message)
REM This is Log4Net *********
2011-04-03 10:30:52,871 ERROR- #499577 Dilshan flings himself to send Kohli back!
2011-04-03 10:30:52,911 INFO - #499577 MS Dhoni walks in to steady the innings.

REM and here is EntLib *********
4/3/2011 10:29:44 AM : Information - #499773 MS Dhoni walks in to steady the innings.
4/3/2011 10:29:44 AM : Critical - #499774 Dilshan flings himself to send Kohli back!

Showdown!
Things that I noted from this limited experiment:
  • EntLib has a nice config tool. Log4net could have one too but I couldn't find any. To be fair, the xml muck for log4net is simpler to write by hand and shorter by about 30%.
  • It's easy to tweak the amount of logging via an xml file edit. You tweak the 'Minimum Severity property for the Logging category in EntLib5.0 ; the root/level element's value attribute in Log4Net.
  • Log4Net is 3x-5x faster from the rolling file burst logging tests that I did. Although it probably doesn't matter for most purposes.
  • Log4Net also has a smaller assembly footprint - you just need one DLL. EntLib will drag in 4 dependencies ; additional EntLib DLLs.
logger.Info("We just won the ICC World Cup! Yay!! Logging out...")

2 comments:

  1. Bonjour,
    Comment changer dynamiquement le ficher du log

    ReplyDelete
    Replies
    1. It seems you can change it at run time by modifying the app.config programatically. Look at the ConfigurationManager class and search online for 'programatically access Enterprise Library logging'

      Delete