Microsoft KB Archive/313417

= How to trace and debug in Visual Basic 2005 or in Visual Basic .NET =

Article ID: 313417

Article Last Modified on 6/1/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q313417







For a Microsoft Visual Basic 6.0 version of this article, see 161153.



SUMMARY
This article demonstrates how to use the Debug and the Trace classes. These classes are available in the Microsoft .NET Framework. You can use these classes to provide information about the performance of an application either during application development or after deployment to production. These classes are only one part of the instrumentation features that are available in the .NET Framework.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 or Microsoft Windows XP
 * Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET

This article also assumes that you are familiar with program debugging.

Description of the technique
The steps in the &quot;Create a sample with the Debug class&quot; section demonstrate how to create a console application that uses the Debug class to provide information about the program execution.

When the program runs, you can use methods of the Debug class to produce messages that help to monitor, to detect malfunctions, or to provide performance measurement information. By default, the messages that the Debug class produces appear in the Output window of the Microsoft Visual Studio Integrated Development Environment (IDE).

The sample code uses the WriteLine method to produce a message that is followed by a line terminator. When you use this method to produce a message, each message appears on a separate line in the Output window.

If you use the Assert method of the Debug class, the Output window displays a message only if a specified condition evaluates to false. The message also appears in a modal dialog box to the user. The dialog box includes the message, the project name, and the Debug.Assert statement number. The dialog box also includes three command buttons:
 * Abort: The application stops running.
 * Retry: The application enters debug mode.
 * Ignore: The application proceeds.

The user must click one of these buttons before the application can continue.

You can also direct output from the Debug class to destinations other than the Output window. The Debug class has a collection named Listeners that includes Listener objects. Each Listener object monitors Debug output and directs the output to a specified target. Each Listener in the Listeners collection receives any output that the Debug class generates. Use the TextWriterTraceListener class to define Listener objects. You can specify the target for a TextWriterTraceListener class through its constructor. Some possible output targets include:
 * The Console window by using the System.Console.Out property.
 * A text (.txt) file by using the System.IO.File.CreateText(&quot;FileName.txt&quot;)) statement.

After you create a TextWriterTraceListener object, you must add the object to the Debug.Listeners collection to receive Debug output.

Create a sample with the Debug class
 Use Visual Basic 2005 or Visual Basic .NET to create a new Console Application project named conInfo. A public module named Module1 is added to the project by default.  To initialize variables to contain information about a product, add the following Dim statements: Dim sProdName As String = &quot;Widget&quot; Dim iUnitQty As Integer = 100 Dim dUnitCost As Decimal = 1.03   Specify the message that the class produces as the first input parameter of the WriteLine method. Press the CTRL+ALT+O key combination to ensure that the Output window is visible. Debug.WriteLine(&quot;Debug Information-Product Starting &quot;)   For readability, use the Indent method to indent subsequent messages in the Output window: Debug.Indent   To display the content of selected variables, use the WriteLine method as follows: Debug.WriteLine(&quot;The product name is &quot; & sProdName) Debug.WriteLine(&quot;The available units on hand are &quot; & iUnitQty) Debug.WriteLine(&quot;The per unit cost is &quot; & dUnitCost)   You can also use the WriteLine method to display the namespace and the class name for an existent object. For example, the following code displays the System.Xml.XmlDocument namespace in the Output window: Dim oxml As New System.Xml.XmlDocument Debug.WriteLine(oxml) </li>  To organize the output, you can include a category as an optional, second input parameter of the WriteLine method. If you specify a category, the format of the Output window message is &quot;category: message.&quot; For example, the first line of the following code displays &quot;Field: The product name is Widget&quot; in the Output window: Debug.WriteLine(&quot;The product name is &quot; & sProdName, &quot;Field&quot;) Debug.WriteLine(&quot;The units on hand are &quot; & iUnitQty, &quot;Field&quot;) Debug.WriteLine(&quot;The per unit cost is &quot; & dUnitCost, &quot;Field&quot;) Debug.WriteLine(&quot;Total Cost is&quot; & iUnitQty * dUnitCost, &quot;Calc&quot;) </li>  The Output window can display messages only if a designated condition evaluates to true by using the WriteLineIf method of the Debug class. The condition to be evaluated is the first input parameter of the WriteLineIf method. The second parameter of WriteLineIf is the message that appears only if the condition in the first parameter evaluates to true. Debug.WriteLineIf(iUnitQty > 50, &quot;This message WILL appear&quot;) Debug.WriteLineIf(iUnitQty < 50, &quot;This message will NOT appear&quot;) </li>  Use the Assert method of the Debug class so that the Output window displays the message only if a specified condition evaluates to false: Debug.Assert(dUnitCost > 1, &quot;Message will NOT appear&quot;) Debug.Assert(dUnitCost < 1, &quot;Message will appear&quot;) </li>  Create the TextWriterTraceListener objects for the Console window (tr1) and for a text file named Output.txt (tr2), and then add each object to the Debug Listeners collection: Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New _ TextWriterTraceListener(System.IO.File.CreateText(&quot;Output.txt&quot;)) Debug.Listeners.Add(tr2) </li>  For readability, use the Unindent method to remove the indentation for subsequent messages that the Debug class generates. When you use the Indent and the Unindent methods together, the reader can distinguish the output as group. Debug.Unindent Debug.WriteLine(&quot;Debug Information-Product Ending&quot;) </li>  To ensure that each Listener object receives all of its output, call the Flush method for the Debug class buffers: Debug.Flush </li></ol>

Using the Trace class
You can also use the Trace class to produce messages that monitor the execution of an application. The Trace and Debug classes share most of the same methods to produce output, including:
 * WriteLine
 * WriteLineIf
 * Indent
 * Unindent
 * Assert
 * Flush

You can use the Trace and the Debug classes separately or together in the same application. In a Debug Solution Configuration project, both Trace and Debug output are active. The project generates output from both of these classes to all Listener objects. However, a Release Solution Configuration project only generates output from a Trace class. The Release Solution Configuration project ignores any Debug class method invocations. Trace.WriteLine(&quot;Trace Information-Product Starting &quot;)

Trace.Indent

Trace.WriteLine(&quot;The product name is &quot; & sProdName) Trace.WriteLine(&quot;The product name is &quot; & sProdName, &quot;Field&quot;) Trace.WriteLineIf(iUnitQty > 50, &quot;This message WILL appear&quot;) Trace.Assert(dUnitCost > 1, &quot;Message will NOT appear&quot;)

Trace.Unindent Trace.WriteLine(&quot;Trace Information-Product Ending&quot;) Trace.Flush Console.ReadLine

Verify that it works
<ol> Make sure that Debug is the current solution configuration.</li> If the Solution Explorer window is not visible, press the CTRL+ALT+L key combination to display this window.</li> Right-click conInfo, and then click Properties.</li> In the left pane of the conInfo property page, under the Configuration folder, make sure that the arrow points to Debugging.</li> Above the Configuration folder, in the Configuration drop-down list box, click Active (Debug) or Debug, and then click OK.</li> Press CTRL+ALT+O to display the Output window.</li> Press the F5 key to run the code. When the Assertion Failed dialog box appears, click Ignore.</li> In the Console window, press ENTER. The program should finish, and the Output window should display the following output:

<pre class="fixed_text">Debug Information-Product Starting The product name is Widget The available units on hand are 100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are 100 Field: The per unit cost is 1.03 Calc: Total cost is 103 This message WILL appear DEBUG ASSERTION FAILED Assert Short Message Message will appear Assert Long Message

at Module1.Main C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\conInfo\Module1.vb(29)

The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending

</li> The Console window and the Output.txt file should display the following output:

<pre class="fixed_text">(The Output.txt file is located in the same directory as the conInfo executable, conInfo.exe. Normally this is the \bin folder of where the project source has been stored. By default that would be C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin) The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending

</li></ol>

Complete code listing
Module Module1 Sub Main Dim sProdName As String = &quot;Widget&quot; Dim iUnitQty As Integer = 100 Dim dUnitCost As Decimal = 1.03

Debug.WriteLine(&quot;Debug Information-Product Starting &quot;) Debug.Indent

Debug.WriteLine(&quot;The product name is &quot; & sProdName) Debug.WriteLine(&quot;The available units on hand are &quot; & iUnitQty) Debug.WriteLine(&quot;The per unit cost is &quot; & dUnitCost)

Dim oxml As New System.Xml.XmlDocument Debug.WriteLine(oxml)

Debug.WriteLine(&quot;The product name is &quot; & sProdName, &quot;Field&quot;) Debug.WriteLine(&quot;The units on hand are &quot; & iUnitQty, &quot;Field&quot;) Debug.WriteLine(&quot;The per unit cost is &quot; & dUnitCost, &quot;Field&quot;) Debug.WriteLine(&quot;Total cost is &quot; & iUnitQty * dUnitCost, &quot;Calc&quot;)

Debug.WriteLineIf(iUnitQty > 50, &quot;This message WILL appear&quot;) Debug.WriteLineIf(iUnitQty < 50, &quot;This message will NOT appear&quot;)

Debug.Assert(dUnitCost > 1, &quot;Message will NOT appear&quot;) Debug.Assert(dUnitCost < 1, &quot;Message will appear&quot;)

Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1)

Dim tr2 As New _ TextWriterTraceListener(System.IO.File.CreateText(&quot;Output.txt&quot;)) Debug.Listeners.Add(tr2)

Debug.WriteLine(&quot;The product name is &quot; & sProdName) Debug.WriteLine(&quot;The available units on hand are &quot; & iUnitQty) Debug.WriteLine(&quot;The per unit cost is &quot; & dUnitCost)

Debug.Unindent Debug.WriteLine(&quot;Debug Information-Product Ending&quot;)

Debug.Flush

Trace.WriteLine(&quot;Trace Information-Product Starting &quot;)

Trace.Indent

Trace.WriteLine(&quot;The product name is &quot; & sProdName) Trace.WriteLine(&quot;The product name is &quot; & sProdName, &quot;Field&quot;) Trace.WriteLineIf(iUnitQty > 50, &quot;This message WILL appear&quot;) Trace.Assert(dUnitCost > 1, &quot;Message will NOT appear&quot;)

Trace.Unindent Trace.WriteLine(&quot;Trace Information-Product Ending&quot;)

Trace.Flush

Console.ReadLine

End Sub End Module

Troubleshooting
<ul> <li>If the solution configuration type is Release, the Debug class output is ignored.</li> <li>After you create a TextWriterTraceListener class for a particular target, TextWriterTraceListener receives output from the Trace and the Debug classes. This occurs regardless of whether you use the Add method of the Trace or the Debug class to add TextWriterTraceListener to the Listeners class.</li> <li> If you add a Listener object for the same target in the Trace and the Debug classes, each line of output is duplicated, regardless of whether Debug or Trace generates the output. Dim tr1 As New TextWriterTraceListener(System.Console.Out) Debug.Listeners.Add(tr1) Dim tr2 As New TextWriterTraceListener(System.Console.Out) Trace.Listeners.Add(tr2) </li></ul>

<div class="references_section">