My favourite log4net config settings

It occurred to me, tonight, as I copied and pasted the same generic log4net config to a new project that I do this because this is my favourite log4net configuration. And, if it's a good enough starting point for all my projects then it may be of interest to others.

 

This config writes to two appenders; the console and a file appender. The file appender is pretty neat in that it;

  • Creates log files in a logs/ folder
  • The standard file is "general.txt"
  • This file grows on each iteration, until it reaches a maximum size of 2MB
  • When it hits 2MB, the file is renamed and a new "general.txt" file is opened
  • Maintains a maximum of 10 renamed / archived log files (so, a total of 20MB of logging data)

I find that this gives me enough info for immediate logging, but also allows me to look back on a number of previous iterations to compare output.

Of course, being log4net and XML driven, all of these parameters are easily changed.

So, let's get to it. To allow log4net to read the configuration, you need to add the declaration to your <configuration> section in app.config / web.config;

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

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

  </configSections>

 

Then, further down, declare the log4net configuration;

 

  <log4net>

    <root>

      <level value="WARN"/>

      <appender-ref ref="RollingFileAppender" />

      <appender-ref ref="ConsoleAppender" />

    </root>

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >

      <file value="Logs/general.txt" />

      <appendToFile value="true" />

      <rollingStyle value="Size" />

      <maxSizeRollBackups value="10" />

      <maximumFileSize value="2000KB" />

      <staticLogFileName value="true" />

      <layout type="log4net.Layout.PatternLayout">

        <param name="Header" value="&#13;&#10;[Session starts]&#13;&#10;"/>

        <param name="Footer" value="&#13;&#10;[Session ends]&#13;&#10;"/>

        <param name="ConversionPattern"

        value="%d [%t] %-5p %c.%M() [%x] &lt;%X{auth}&gt; – %m%n"/>

      </layout>

    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; – %m%n"/>

      </layout>

    </appender>

    <logger name="kwoloBackup.Service.Program">

      <level value="INFO" />

    </logger>

    <logger name="kwoloBackup.Provider">

      <level value="DEBUG" />

    </logger>

    <logger name="kwoloBackup.Provider.BackupProvider">

      <level value="DEBUG" />

    </logger>

    <logger name="theCmdb.PacketGatherer">

      <level value="INFO" />

    </logger>

    <logger name="theCmdb.PacketGathererService">

      <level value="INFO" />

    </logger>

  </log4net>

</configuration>

 

This should be self-explanatory (post a comment if I'm completely wrong!) but I'll offer some highlights;

  1. The inital <level value="WARN"/> entry is the default  log level, where we don't have a class specific log level later on
  2. <file value="Logs/general.txt" /> is the base name of the log file; note we specify a folder and file name
  3. <maximumFileSize value="2000KB" /> is (perhaps obviously!) the maximum size we'lll let general.txt get to
  4. <maxSizeRollBackups value="10" /> is the number of backup files we'll hold onto
  5. The bit that begins <layout type="log4net.Layout.PatternLayout"> bounds each program iteration with the date/time it started and ended

FInally, the sets of config that looks like this;

  <logger name="kwoloBackup.Service.Program">

      <level value="INFO" />

  </logger>

…set the logging level for modules running under that name. For example, when you instantiate an ILog object within your module with a specific name, then it's these <logger name="…">  entries that are checked for the closest match.

So, if I have a class that defines an ILog object like this;

  private static readonly ILog logger = LogManager.GetLogger("kwoloBackup.Service.Program");

Then, with the above config, the logger will be running at "INFO" level.

Just for your info, if you have another class that creates an ILog with;

  private static readonly ILog logger = LogManager.GetLogger("kwoloBackup.Service.Program.MegaFunction");

then, if you don't explicitly have a configuration section defined for "kwoloBackup.Service.Program.MegaFunction" then this will inherit from the "kwoloBackup.Service.Program" section. In other words, it gets "INFO" level logging too.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">