log4net – Logging for your .Net Apps!

Ok I’m no log4net expert but it is a very cool package that everyone should use – believe me – it’s very quick to chuck in and will save you mountains of time and make you look like a pro without even really trying 🙂 It’s been around for along time (google for lots of tutorials etc) but I thought I’d put up my most basic x step guide to get running with log4net (this is once again so I can remember too 🙂 ):

1) Grab the log4net dll and register it in the project you want to use it in. (http://sourceforge.net/project/showfiles.php?group_id=31983&release_id=171808)

2) Make up a static class / singleton and chuck something like this in it:

private static ILog _log;


_log = LogManager.GetLogger(“GlobalLogging”);


public static ILog Log
     get { return _log; }

3) Chuck a section like this in your main app.config:

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

 <appender name=”FileAppender” type=”log4net.Appender.FileAppender”>
  <file value=”Log.txt” />
  <appendToFile value=”true” />
  <layout type=”log4net.Layout.PatternLayout”>
   <conversionPattern value=”%date [%thread] %-5level %logger – %message%newline” />

 <appender name=”ConsoleAppender” type=”log4net.Appender.ConsoleAppender” >
  <layout type=”log4net.Layout.PatternLayout”>
   <conversionPattern value=”%date [%thread] %-5level %logger – %message%newline” />

  <level value=”INFO” />
  <appender-ref ref=”FileAppender” />
  <appender-ref ref=”ConsoleAppender” />

<logger name=”GlobalLogging”>
 <level value=”INFO” />
 <appender-ref ref=”FileAppender” />

Note: Get rid of the console appender if you just want to chuck the logs to a file.

4) Use your logger: Helper.Log.Info(“This is a log message”);

Now there are different types of messages (warn, info, debug etc) so these types need to be specified in the config – just add <level /> nodes in for the ones you want to log – if they arent in there then they get ignored and wont be written to the file 🙂 That’s how you can say do logging on development (debug) and then turn those off on production without changing your code! Just remove your setting from the config!

Another trick is to use logger sections with named loggers (note the “GlobalLogging”). By getting rid of the <root /> node and only having the logger node you can filter the messages down. This is useful if other components also perform logging but you dont want there messages to come through i.e. it can be used as a filter 🙂

Anyway, its really easy to use and you will get the hang of it “just like that” 🙂 – definitelly worth much more than the 2 minutes it takes to chuck in!


2 Responses to “log4net – Logging for your .Net Apps!”

  1. FHA Approved Condos Says:

    good stuff.

  2. logview4net Says:


    […]log4net – Logging for your .Net Apps! « Cache Dump[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: