RollingPatternFileAppender - 给线程产生日志文件

Home

RollingPatternFileAppender - RollingFileAppender with dynamic filename

Introduction

RollingPatternFileAppender is an extension for the popular log4net framework. It creates a log file per web session/thread and can roll log files based on size or date or both.

Background

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. RollingPatternFileAppender is an extension for log4net to fit in the web application or multi threads application environment.

Configuration file

RollingPatternFileAppender 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 RollingPatternFileAppender 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.RollingPatternFileAppender">
<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"/>
</appender>

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 asp.net 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(RollingPatternFileAppenderTest)); 


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 asp.net 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;  

Conclusion

A unique log file per session/thread is a great need for debuging web applicaton or multi threads application. RollingPatternFileAppender achieves this by extending the log4net framework. And don't forget the log4net configuration file can be change on the fly. The attached source code include a unit test project that launchs 50 threads in 30 seconds and each one generate a unique log. Enjoy coding!

Download RollingPatternFileAppender source code.

Please
to support this Project.
posted @ 2010-03-30 22:01  zsu  阅读(335)  评论(0)    收藏  举报