Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Debug and Trace Your Code
The .NET Framework's diagnostic classes help you find and fix bugs at both design time and run time.
by Fabio Claudio Ferracchiati

September 2003 Issue

Technology Toolbox: VB.NET

Your application's testing phase is as important as the development and deployment phases. A well-conceived testing plan lets you discover and resolve bugs and logical errors. The .NET Framework's diagnostic classes—Debug and Trace in the System.Diagnostics namespace—make testing applications and finding bugs easier at both design time and run time. I'll explain what these classes do and how you can take advantage of them in your code.

ADVERTISEMENT

Debug and Trace have the same methods and properties, but you use them differently. You use Debug to display variables' content and debug messages during application design time. You use Trace to trace application information at run time. You can use the compiler's debugging flags to strip debugging and tracing calls from your release code automatically (see Figure 1). Stripping debugging calls makes sense at this stage, but leaving Trace calls in the code even after you deploy the application lets you monitor application behavior and discover bugs by reading tracing information in log files or log applications.

However, the more you fill your code with Trace information, the bigger and slower your applications become. The .NET Framework addresses this dilemma by providing two switch classes that let you activate and deactivate tracing functionality simply by changing a value in the application configuration file. As I'll explain in more detail later, the BooleanSwitch and TraceSwitch classes check a Boolean value and a numeric trace level parameter to inform the Trace methods when to activate and deactivate code instrumentation, respectively. Your application is still larger than it would be without Trace class functionality, but you improve its performance by using the switch classes.

The System.Diagnostics namespace also provides classes that redirect the output of debugging and tracing information to a listener application. VS.NET's Output window is the default listener, but you can use the EventLogListener and TextWriterTraceListener classes to change it. EventLogListener redirects output to the Windows Event Log file, which you can examine with the Event Viewer tool. TextWriterTraceListener redirects the output to a text file.

The Trace class provides six methods (see Table 1). All these methods—the same as those in the Debug class—are shared in VB.NET (static in the other languages), so you don't need to create a new object from the Trace class. This code writes a message to the Output window when the user clicks on an application's Close button:

Imports System.Diagnostics

Public Class frmMain
   Inherits System.Windows.Forms.Form

   Private Sub btnClose_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnClose.Click
   Trace.WriteLine( _
      "The application has been " & _
      "closed")
      Application.Exit()
   End Sub


Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home