Wednesday, January 25, 2012

log4Net: RollingFileAppender Class

What is log4net?
Apache log4net from their website: "The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime."

What is the RollingFileAppender?
RollingFileAppender exists within the log4net in the log4net.Appender namespace. According to the website it's designed to append "log files based on size or date or both.". A pretty basic description, but it does describe what this logging type does: it appends we log messages to an existing log (or creates a new one if none exists), and when it reaches the roll condition it creates a new file.

The conditions under which a roll condition are reached are configured via the RollingStyle  property. You can set the log to role based upon a date, file size, or a combination of the two (known as a composite in the documentation).

Configuration for the RollingFileAppender:
Configuration for the RollingFileAppender is primarily done in an xml configuration file, or your application configuration file.

The good part of a configuration based approach is the ability to customize your logging based upon your life-cycle environment (think QA logging and production logging), or client your individual customer or client's needs.

Log4net also supports run time (or in-code) configuration as well. If your interested in this type of configuration, you can get started here:

In this example, the RollingFileAppender is configured to write the a file named log.txt, with the rolling condition of size, and specifies the maximum file size. One important thing to note is the   in the example below. This will cause log4net to append a count number to the end of the log file as it creates new ones (Example: log.txt.1, log.txt.2, log.txt.3)

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"></appender><br />
    <file value="log.txt"></file><br />
    <appendtofile value="true"></appendtofile><br />
    <rollingstyle value="Size"></rollingstyle><br />
    <maxsizerollbackups value="10"></maxsizerollbackups><br />
    <maximumfilesize value="100KB"></maximumfilesize><br />
    <staticlogfilename value="true"></staticlogfilename><br />
    <layout type="log4net.Layout.PatternLayout"></layout><br />
        <conversionpattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"></conversionpattern>


This example is a little different. It's rolling is determined by the date setting. Note the datePattern used to control the format of the date for file names:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logfile">
    <appendtofile value="true">
    <rollingstyle value="Composite">
    <datepattern value="yyyyMMdd">
    <maxsizerollbackups value="10">
    <maximumfilesize value="1MB">
    <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline">

This represents a typical rolling date configuration section for log4net (thanks to dommer):

    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="c:\Logs\Today.log"/>
        <rollingStyle value="Date"/>
        <datePattern value="yyyyMMdd"/>
        <appendToFile value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %logger %date{ISO8601} - %message%newline"/>
        <!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
        <level value="ERROR"/>
        <appender-ref ref="RollingFile"/>

Consider your larger logging strategy with RollingFileAppender
Every single application should generate log entries. The world is an imperfect place, and the log at a minimum should tell you about those imperfections. However, depending on your logging "enthusiasm" and logging level you are going to write messages. Even a small application can generate a tremendous amount of logging data.

RollingFileAppender can help with that by:

  1. Naturally segmenting your data into smaller files (making a potential search later on) easier
  2. If you use the date RollingStyle then you will have files seperated by day
  3. RollingFileAppender can also manage the destruction of older log files with the maxSizeRollBackups. This will allow you to set the number of archive files to keep online.
If you combine these features with a strategy to potentially compress, and store files (or destroy them with maxSizeRollBackups) then you can have a self-managed and effective strategy to track messages.

... RollingFileAppender full name is log4net.Appender.RollingFileAppender

... Build this into a larger logging and archiving strategy.

... Is log4Net's RollingFileAppender class any better than any other logging libraries rolling file appender? Just is still out.

RollingFileAppender Class
log4net Manual Configuration
log4net Tutorial

1 comment:

Asava Samuel said...

This is even better than log4net: