PatternRollingFileAppender - RollingFileAppender with dynamic filename


PatternRollingFileAppender is an extension for the popular log4net framework. It's a combination of the PatternFileAppender and RollingFileAppender. So you can specify pattern(for example, session id/thread id) in filename and each log file also has all the rolling features provided in RollingFileAppender.


log4net is a popular log framework for .net programmers. It provides many useful appenders but there is no appender that can create log files on the fly based on web session id or thread id, or any dynamic information passed in during runtime. PatternRollingFileAppender is an extension for log4net to fit in the web application or multi threads application environment.

Configuration file

PatternRollingFileAppender inherits all the features from RollingFileAppender. Hence all the configuration settings are exactly the same as the RollingFileAppender except that you can pass in dynamic information for the file name. In the example below the file name is configured as "Session-%property{SessionId}.log" and the %property{SessionId} part will be substituted with the dynamic information passed in during runtime. If you pass in a unique SessionID for eatch web session/thread than a unique log file will be generated per web sessioin/thread; or you can tell the PatternRollingFileAppender to create one log file per user by passing in the user id to the property variable at runtime. You can still control how many backup files to keep per filename by setting the maxSizeRollBackups, maximumFileSize and the rollingStyle.

    <appender name="SessionPatternFileAppender" type="log4net.Appender.PatternRollingFileAppender">
      <file value="Logs\Session-%property{SessionId}.log" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="0" />
      <maximumFileSize value="2MB" />
      <rollingStyle value="Size" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout"
        value="%date %-5level %logger - %message%newline"/>

Using the code

To use log4net framework, the first thing is to load the configuration file just once by calling the XmlConfigurator.ConfigureAndWatch function when the application starts, In application, Applicaton_Start event is the best place to put the following code.

 XmlConfigurator.ConfigureAndWatch(new FileInfo(Util.LogConfigFilePath)); 

Then in the class where you want to use the log, create an ILog object by calling the LogManager.GetLogger.

private static ILog _log = LogManager.GetLogger(typeof(PatternRollingFileAppenderTest)); 

Now you need to pass in the dynamic information by setting the log4net.ThreadContext.Properties["variablename"] to substitue the placeholder in the filename. To pass in the SessionId in application, you can place the following codes in Application_AcquireRequestState event and Session_Start event.

if (HttpContext.Current.Session != null) log4net.ThreadContext.Properties["SessionId"] = HttpContext.Current.Session.SessionID;  

Last edited Jun 4, 2012 at 5:33 PM by sevenzsu, version 2