Improving .NET Trace and Debug with SmartInspect

How It Works
And the Configuration?
Closing Words


The System.Diagnostics.Trace and System.Diagnostics.Debug classes included in the .NET framework provide a set of methods and properties that help you debug and trace your applications. Although their functionality is already helpful on its own, it can be improved by using a monitoring application which is capable of receiving and filtering the logging information. This article shows you how to do exactly that with SmartInspect, our advanced logging tool.

How It Works

Both, the System.Diagnostics.Trace and System.Diagnostics.Debug classes provide the functionality of adding so called TraceListeners. Those TraceListeners are responsible for writing the logging information to the actual destination, like a log file or standard output. Now, to get SmartInspect to cooperate with the Trace and Debug classes, the SmartInspect .NET library provides its own TraceListener which is named SmartInspectTraceListener.

This listener directs the logging output of the Debug and Trace classes to a SmartInspect session which in turn is capable of sending it to a SmartInspect log file or directly to the SmartInspect Console. I think here's a good place for a short example:

using System;
using System.Diagnostics;
public class Program { public static void Main(string[] args) { Debug.Indent(); Debug.Write("Hello, World!"); Debug.Fail("Test Failure"); Debug.Unindent(); Debug.Write("Bye!"); } }

This piece of code should be familiar to you. It just demonstrates the usage of the Debug class in general (the usage of the Trace class is similar), without SmartInspect support so far. Now, to redirect the logging output to a SmartInspectTraceListener instead of using the default listener, we only need to add a few lines of code:

// ..
using Gurock.SmartInspect; using Gurock.SmartInspect.DebugTrace;
public class Program { static Program() { SiAuto.Si.Enabled = true; Debug.Listeners.Clear(); // Remove default listener Debug.Listeners.Add(new SmartInspectTraceListener()); }
// .. }

As you can see, in the static constructor we just enable SmartInspect logging, remove the default listener and add our own. That's it. There's no need to touch the existing log statements, so the Main method is still the same. When running the example, the logging output in the SmartInspect Console then looks like:

The Debug output redirected to the SmartInspect Console The Debug output redirected to the SmartInspect Console

When using the Trace and Debug classes in combination with SmartInspect, you are now able to filter, search and even edit your logs. And, I can't say that often enough, without changing your existing log statements.

And the Configuration?

The configuration can either be done programmatically like in the example above or by using the application's configuration file. When using the configuration file option, you have the advantage of being more flexible. That means you do not need to recompile your application just for changing the application logging. An example configuration file could look like:

         <clear /> <!-- remove default listener -->
              SmartInspectTraceListener, Gurock.SmartInspect"

At first we remove the default listener again. Then we add our SmartInspectTraceListener with a name of "Test" and by specifying the type and assembly name. Please note that the SmartInspectTraceListener type is wrapped here (see the backslash at the end of the line). It would have exceeded the article width otherwise. Wrapping the type is not valid when using a real configuration file.

Closing Words

Well, that's it. This short article is about to end. I hope it was informative and useful to you. If you have any questions or comments about this topic, feel free to contact me.

Try SmartInspect

Get started in minutes and try SmartInspect free for 30 days.

Try SmartInspect