Playing .NET Doctor: Diagnose Application Hiccups with .NET Classes, Page 2
System.Diagnostics.Trace Sample Code
Because the Debug and Trace classes have the same methods, I just changed the sample code above to use Trace instead of debug. The output will be the same, so I won't bother to include the figures again:
System.Diagnostics.Trace.Assert(false, "Message", "Detail message to show in the debug window.");Random rand = new Random(100);System.Diagnostics.Trace.WriteLine("First random number: " + rand.Next().ToString());System.Diagnostics.Trace.Indent();System.Diagnostics.Trace.WriteLineIf(rand.Next()%2 == 0, "Even random number generated.");System.Diagnostics.Trace.Unindent();
The previous examples are all well and good as long as you're running the code in Visual Studio so that you can see the output window. This may work when debugging on the local machine, but it won't be an option when the application is in production. Thus, you need to be able to control where the output from Debug and Trace statements is written. This is done through what is known as a listener. A listener does just as its name implies: It listens for debug or trace output and then behaves accordingly. You can use a TextWriterTraceListener class to write Debug or Trace output to a TextWriter or a Stream, which can result in output being written to the console, a file, and so forth. You also can use an EventLogTraceListener to send messages to the Windows Event Log. Finally, you have an abstract TraceListener class that you can implement to create custom listeners for monitoring debug and trace output.
Listener Sample Code
The following sample code extends the previously given example code to have the output written to the console window rather than the Output window of the debugger (see Figure 4). It also demonstrates how to send the output just to the console:
// Add a listener to send output to the consoleSystem.Diagnostics.TextWriterTraceListener writer = new System.Diagnostics.TextWriterTraceListener(System.Console.Out);System.Diagnostics.Debug.Listeners.Add(writer);// Remove the default listener and only send to the consoleSystem.Diagnostics.Debug.Listeners.Remove( System.Diagnostics.Debug.Listeners);// Output debug messagesSystem.Diagnostics.Debug.Assert(false, "Message", "Detail message to show in the debug window.");Random rand = new Random(100);System.Diagnostics.Debug.Indent();System.Diagnostics.Debug.WriteLine("First random number: " + rand.Next().ToString());System.Diagnostics.Debug.Unindent();System.Diagnostics.Debug.WriteLineIf(rand.Next()%2 == 0, "Even random number generated.");
Figure 4: Console Output
Beyond the basics of using the Debug and Trace classes covered here, consider the following enhancements:
- Set up a listener through the configuration file—Rather than set up the listener via code, experiment with controlling it through the <listeners> element in the application configuration file.
- Set up the EventLogTraceListener—Configure the listener to output to the event log.
- Implement a custom Listener—Create additional listeners to log to alternative locations such as a database.
The topic of the next column is yet to be determined. If you have something in particular that you would like .NET Nuts & Bolts to explain, contact me at email@example.com.
About the Author
Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C#. You can reach Mark at firstname.lastname@example.org.