Home > Articles > Programming > Windows Programming

Listening to Trace Messages in NUnit

📄 Contents

  1. Using the System.Diagnostics.Trace Class
  2. Using NUnit To Debug .NET Code
  3. Summary
  • Print
  • + Share This
Paul Kimmel shows you how to use .NET's Trace class, write NUnit tests, and eavesdrop on your code while those tests are running.
From the author of

Steve Droke wrote "Knowledge is power and enthusiasm pulls the switch." If you're reading this, you're enthusiastic enough to be seeking knowledge, so let me give you a little.

When it comes to knowledge about software, you can never have enough—because you never know what you're going to need. To this end, we add things like assertions and trace statements to our code. (I hope!) If you want to gain knowledge about your code's behavior, write test cases. For a truly powerful result, run tests in such a way as to permit you to ensure that your code is performing, and to see what it's doing while those tests are running.

In this article, I'll show you how to use .NET's Trace class, write NUnit tests, and eavesdrop on your code while those tests are running. Collectively, you'll gain tremendous insight into what your code is doing, how it's doing it, and whether it's right or not. This is real software development power.

Using the System.Diagnostics.Trace Class

The Trace class is a much more powerful version of the Debug.Print capability found in VB6. Trace permits you to write statements tracing the flow of execution in your program. For example, Trace.WriteLine("acquiring target") will write the text acquiring target to a listener. By default, the listener is the output window in Visual Studio .NET. However, because the Trace class contains a TraceListeners collection, you can add several listeners to the collection. The result is that messages can be multicast to several listeners and custom listeners can be created.

Understanding TraceListeners

To understand TraceListeners, think of a radio station and people who have tuned in that station on their radios (or PCs). The station itself sends out a single broadcast signal and many listeners can receive it. Any device that can "tune in" can receive the broadcast. Completing our analogy, the Trace.Write or Trace.WriteLine static method (shared method for Visual Basic .NET) is the broadcast signal; classes that inherit from TraceListener and instances placed in the Trace.Listeners collection are our metaphorical radio listeners.

Implementing a TraceListener

Implementing a TraceListener is straight forward. To create a custom TraceListener, simply define a new class and override the Write and WriteLine methods, each of which accepts a string argument. Finally, do something with the string. For example, you can write it to a file.


The output window is the default listener, and an EventLogTraceListener and a TextWriterTraceListener already exist. See the Visual Studio .NET help documentation for more information. I'll demonstrate a custom TraceListener a bit later in the code listings for this article.

  • + Share This
  • 🔖 Save To Your Account