Filtering System.Diagnostics Trace messages
February 13, 2012
One of the biggest points earners for me on Stackoverflow has been this question about Error logging in C#/.NET. Having been fairly experienced with the setup of Log4net, I’ve seen firsthand how much over kill it generally is for logging (unless you like to use your live servers for debugging), and also how it just isn’t needed as .NET has its own in built and comprehensive logging framework built in.
One thing that isn’t mentioned in my answer is how to filter your log messages according to error type: information, error, critical etc. It’s fairly easy to do with TraceListeners, but requires quite a few extra lines of XML in your web.config/app.config. The example below shows how to filter information messages, it’s setup to use the eventlog and write to the ASP.NET category, which is necessary with web apps if the user you are running the app pool with doesn’t have admin access, or the registry key for a new event log source doesn’t exist.
<system.diagnostics> | |
<!-- Add multiple sources for varying levels of tracing --> | |
<sources> | |
<source name="debugSource" switchName="defaultSwitch" switchType="System.Diagnostics.SourceSwitch"> | |
<listeners> | |
<add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" /> | |
</listeners> | |
</source> | |
</sources> | |
<switches> | |
<add name="defaultSwitch" value="Error"/> | |
<!-- See System.Diagnostics.SourceLevels enum --> | |
</switches> | |
<trace autoflush="false" indentsize="4"> | |
<listeners> | |
<add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" /> | |
</listeners> | |
</trace> | |
</system.diagnostics> |
You should be using a wrapper/Façade class for the logging so you can have a single entry point and class with one responsibility in your app. Checking for the output level is then easy:
private TraceSource _traceSource = new TraceSource("defaultSource"); public void LogInformation(string message,object params args) { if ((_traceSource.Switch.Level & SourceLevels.Information) == SourceLevels.Information) { ... } }
I'm Chris Small, a software engineer working in London. This is my tech blog. Find out more about me via Github, Stackoverflow, Resume