Using tracing in .NET

A short introduction to using the tracing method of debugging in .NET.

Over the last ten years or so, programmers have been using source level debuggers and IDEs to fix mistakes in their

code. Before that programmers had to add tracing statements in their code to help deduce problems. Now tracing is back in .NET. In this excerpt from InformIT, .NET Reference Guide host Jim Mischel provides an explanation of tracing in .NET.


The .NET Framework has an integrated tracing mechanism that allows you to add trace output statements to your program and then selectively enable those statements at runtime by setting values in the application's configuration file. The Framework allows you to define your own classes that collect the trace information and send it to whatever output device you choose. Two very similar classes make use of trace output:

  • The Debug class is enabled when you compile with the DEBUG conditional enabled. In Visual Studio .NET, the DEBUG conditional is enabled by default when you compile a debug build.
  • The TRACE conditional is enabled by default in both debug and release builds, and enables the use of tracing output through the Trace class.

In theory, you would use Debug output for messages that you only want to view during debugging -- that is, during program development and never in production. You would use Trace output statements for messages that you might want to view after the program has been placed in production. In practice, I've found it much more convenient to ignore Debug and use only Trace output, reserving the Verbose trace level (described below) for debug-level messages.

Although the following discussion describes only the Trace class, the discussion applies equally to the Debug class unless otherwise noted. The process of adding trace statements to your program is called instrumentation. The Trace class defines six different output methods:

  • Assert: Outputs the specified text (or the call stack if no text is specified) if the condition supplied in the first argument evaluates to false.
  • Fail: Outputs the specified text (or the call stack if no text is specified). This method is typically used inside an error-handling code block. Write Outputs the specified text.
  • WriteIf: Outputs the specified text if the condition supplied in the first argument evaluates to true. WriteLine Outputs the specified text and a carriage return.
  • WriteLineIf Outputs the specified text and a carriage return, only if the condition supplied in the first argument evaluates to true.

The Assert and Fail methods not only output messages, but also pop up dialog boxes that display an error message and allow you to Abort the program, Retry the operation, or Ignore the error. You probably do not want to use Assert and Fail in programs that are supposed to run unattended.


Read more about tracing in InformIT's .NET Reference Guide.


This was first published in February 2004

Dig deeper on Win Development Resources

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close