Benjamin Ortuzar
Benjamin Ortuzar

Reputation: 7831

Reading log from Log4Net generated by multiple threads

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.

Upvotes: 9

Views: 2065

Answers (2)

Athens Holloway
Athens Holloway

Reputation: 2203

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://codereport.net/logging-the-thread-name-with-log4net/

Upvotes: 7

Peter Lillevold
Peter Lillevold

Reputation: 33950

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.

Upvotes: -1

Related Questions