Microsoft KB Archive/815788

= How to trace and debug in Visual C# =

Article ID: 815788

Article Last Modified on 5/17/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 313417.



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Diagnostics

IN THIS TASK

 * SUMMARY
 * Requirements
 * Description Of Technique
 * Create a Sample with the Debug Class
 * Using the Trace Class
 * Verify That It Works
 * Complete Code Listing
 * Troubleshoot
 * REFERENCES



SUMMARY
This article describes 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.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:


 * Microsoft Windows 2000 or Microsoft Windows XP or Microsoft Windows Server 2003
 * Microsoft Visual C# .NET or Microsoft Visual C# 2005

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

back to the top

Description Of Technique
The steps in the Create a Sample with the Debug Class section demonstrate how to create a console application that uses the Debug class to provide information about the program execution.

When the program is run, you can use methods of the Debug class to produce messages that help you to monitor the program execution sequence, 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 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.

When 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 the following 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 theDebug 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 Listener 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 following:
 * 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.

back to the top

Create a Sample with the Debug Class
 Start Visual Studio .NET or Visual Studio 2005. Create a new Visual C# Console Application project named conInfo. Class1 is created in Visual Studio .NET. Program.cs is created in Visual Studio 2005.  Add the following namespace at top in Class1 or Program.cs. using System.Diagnostics;   To initialize variables to contain information about a product, add the following declaration statements to Main method: string sProdName = &quot;Widget&quot;; int iUnitQty = 100; double dUnitCost = 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 make sure 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; </li>  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.ToString); Debug.WriteLine(&quot;The per unit cost is &quot; + dUnitCost.ToString); </li>  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: System.Xml.XmlDocument oxml = 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.ToString,&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 since dUnitcost < 1 is false&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: TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(tr1); TextWriterTraceListener tr2 = 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 make sure that each Listener object receives all its output, call the Flush method for the Debug class buffers: Debug.Flush; </li></ol>

back to the top

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 the following:
 * 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; back to the top

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: Debug Information-Product Starting The product name is Widget The available units on hand are100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are100 Field: The per unit cost is1.03 Calc: Total Cost is 103 This message WILL appear DEBUG ASSERTION FAILED Assert Short Message Message will appear since dUnitcost < 1 is false Assert Long Message

at Class1.Main(String[] args) <%Path%>\class1.cs(34)

The product name is Widget The available units on hand are100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name isWidget This message WILL appear Trace Information-Product Ending </li> <li> The Console window and the Output.txt file should display the following output: 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>

Note The Output.txt file is located in the same directory as the conInfo executable (conInfo.exe). Typically, this is the \bin folder where the project source is stored. By default, this is C:\Documents and Settings\User login\My Documents\Visual Studio Projects\conInfo\bin.

back to the top

Complete Code Listing
using System; using System.Diagnostics;

class Class1 {     [STAThread] static void Main(string[] args) {        string sProdName = &quot;Widget&quot;; int iUnitQty = 100; double dUnitCost = 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.ToString); Debug.WriteLine(&quot;The per unit cost is &quot;+ dUnitCost.ToString);

System.Xml.XmlDocument oxml = 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.ToString,&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 since dUnitcost < 1 is false&quot;);

TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(tr1); TextWriterTraceListener tr2 = 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; }           } Note For this sample code to work, you must add the System.Diagonstics namespace by pasting using System.Diagnostics; as the first line of your code.

back to the top

Troubleshoot
<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 Listeners 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. TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out); Debug.Listeners.Add(myWriter); TextWriterTraceListener myCreator = new TextWriterTraceListener(System.Console.Out); Trace.Listeners.Add(myCreator); </li></ul>

back to the top

<div class="references_section">