Skip to main content Link Menu Expand (external link) Document Search Copy Copied

By default, log4net exposes 4 log levels: ERROR, WARNING, INFO and DEBUG so I tend to use them in the following way:

  • ERROR: only for unexpected exceptions. i.e. events that may indicate that the program state is incorrect.
  • WARN: only for events that may indicate intervention is going to be required. i.e. queues are getting large or execution times are getting to unacceptable limits. Care is taken to ensure that large numbers of these aren't streamed into the log.
  • INFO: high level state changes. i.e. opening a TCP listening socket or a summary of processing events. Again, care is taken to ensure that large numbers of the these aren't written to the log.
  • DEBUG: everything else.

ERROR and WARN are special events indicating something has gone wrong which leaves INFO and DEBUG. I like to have production applications logging a minimal amount during normal operation so I choose INFO as the default log level. This means that I can open a log and quickly get a feel for what state it’s in.

Unfortunately this only leaves me with one level of logging granularity for debugging which I find a little restrictive. I like to be able to turn up the logging a little to get more of a feel for what is going on and then turn it up again to see everything else and for this I’d need two levels after INFO. DEBUG and another, lets call it VERBOSE.

To put this into context lets imagine a processing job that operates periodically to process a queue of work. I’d use the log levels in the following way.

  • INFO: one summary log entry after the job has completed processing with details for the number of items processed, how long it took and any other relevant statistics about the work performed.
  • DEBUG: completion of each item with details about the item that was processed.
  • VERBOSE: the processing steps for each item of queued work.

This is when extra log levels would come in handy and the log4net designers realised this. Here’s the full set of levels defined by log4net:

  • Emergency
  • Fatal
  • Alert
  • Critical
  • Severe
  • Error
  • Warn
  • Notice
  • Info
  • Debug (also defined as Fine)
  • Trace (also defined as Finer)
  • Verbose (also defined as Finest)

It looks like they couldn’t decide on some of the names so Debug and Fine are defined with the same severity and can be used interchangably. This is the same with Trace and Finer and with Verbose and Finest. Note though that while they are interchangable for severity filtering purposes, the text of the specific log level used will be printed in the log.

Now you may have noticed that you can’t see these extra levels on the ILog interface. This is because the interface is kept simple for when you only need the common four levels. You get to the rest through the Logger property of the ILog interface. It’s not pretty but you can neaten things up with a few extension methods. Here’s an example of the Verbose level which would be easy to replicate for the other levels and overloads.

public static class ILogExtentions
    {
        public static void VerboseFormat(this log4net.ILog log, string format, params object[] args)
        {
            Exception exception = null;
            log.Logger.Log(
                System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
                log4net.Core.Level.Verbose,
                string.Format(format, args),
                exception);
        }
    }

Here’s an example usage of the new method:

log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(Program));
Logger.VerboseFormat("Verbose log entry");

Note that these code snippets came from Matthew Lowrance’s blog which doesn’t seem to be live anymore.