views:

270

answers:

2

I have a script that has multiple threads running in parallel. These threads write to a Log4Net RollingFileAppender file. Reading this log is a quite confusing since all the thread logs are mixed up. Im wondering what is a good way to write these logs, and what is the best way to read these files so reading the debugging information of a particular thread becomes easier.

+6  A: 

Update your config file to include the thread name in the log output. If you set the Threads name in code, that same name will be logged to your file. This is a simple example of including the thread name via the log4net config file <conversionPattern> tag:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="service.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="2MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        **<conversionPattern value="%-5level : [%t] - %message%newline" />**
    </layout>
</appender>

UPDATE I wrote a simple POC app to demonstrate this. http://www.codereport.net/2010/03/logging-thread-name-with-log4net.html

Athens
Thread.Name MSDN link: http://msdn.microsoft.com/en-us/library/system.threading.thread.name.aspx
Athens
I have added the thread information and then I am using LogExpert (http://www.log-expert.de/) to filter the log to read the trace of each thread easily.
Benjamin Ortuzar
A: 

Consider logging to something easier to handle, like a database. Using the AdoNetAppender logging to a database table you can easily sort and filter on the thread.

Over at the log4net site there are config samples on how to get this going.

Peter Lillevold