Log4net TraceListener

by Geert 18. June 2010 12:57

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: 553]

kick it on DotNetKicks.com

Trace listener class for log4net

Tags:

C#

Comments are closed

About the Author

Geert van Horrik is a independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!