Log4net TraceListener

Until now, I used the static Trace class to log information of applications to a log file. However, recently a collegue recommended log4net since it supports formatting options of the log file and to set the maximum filesize of the logfile (the logfiles we received were very big). Also, it supports a lot of trace helper methods I wrote to trace exceptions with information.

However, the whole application has tracecs, and I didn’t want to change all of the calls just for the log4net change. Therefore, I wrote the Log4netTraceListener class to forward all traces to log4net.

This is how to use the class:

1) Add the file to your application or core library if you prefer that.

2) Add the trace listener to the app.config file:

<!-- Diagnostics settings -->
<system.diagnostics>    
    <!-- All your other options here -->
    <trace autoflush="true">   
      <listeners>    
        <add name="Log4netTraceListener" type="CatenaLogic.Diagnostics.Log4netTraceListener, MyAssembly" />
      </listeners>  
    </trace>
</system.diagnostics>

Log4netTraceListener.cs (6.62 kb) [Downloads: 81]

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList
Comments are closed