815788 HOW TO: Tracking and debugging in Visual C # .NET (from mkba)

xiaoxiao2021-03-06  76

For Microsoft Visual Basic .NET versions of this article, see

313417.

This article refers to the following Microsoft .NET Framework Class library namespace:

System.Diagnostics

This task content

summary

Require Method Description Use the Debug class to create an example using the Trace class to confirm that it can use a full code list troubleshooting reference

Summary This article describes how to use

Debug and

Trace class. These two classes are available in Microsoft .NET Framework. These two classes provide information about application performance during application development or deployment to the product. These two classes are only part of the configuration functionality provided in .NET Framework.

Back to top

Require the following list summarizes the recommended hardware, software, network structure, and the required service pack:

Microsoft Windows 2000 or Microsoft Windows XP Microsoft Visual C # .NET also assumes that you are familiar with program debugging.

Back to top

Method description

in

Create a step in an example section using the Debug class Demo how to create usage

The DEBUG class is to provide a console application for program execution information.

You can use it when the program is running

The method of the Debug class generates a message to help you monitor the program execution order, detect malfunctions or provide performance metrics. by default,

The message generated by the DEBUG class is displayed in the "Output" window of the Visual Studio integration development environment (IDE).

This code example is used

The WriteLine method generates a message with row end character. When you use this method to generate a message, each message is displayed in a single line in the Output window.

If used

Debug class

The "Output" window displays the message only when the specified condition is calculated as false. This message is also displayed in a mode dialog. The dialog includes messages, project names and

DEBUG.ASSERT statement number. The dialog includes the following three command buttons:

Termination: The application stops running. Retry: The application enters debug mode. Ignore: The application continues. Users must first click on one of these buttons and then the application can continue.

You can also indicate

The DEBUG class outputs the target other than the Output window.

The Debug class has a name

Collection of Listeners, this collection includes some

Listener object.

Each

Listener objects are monitored

DEBUG outputs and points the output to the specified target.

Each of the Listener collection

Listener receives

Any output generated by the DEBUG class. please use

TextWritertraceListener class definition

Listener object. able to pass

The constructor of the TextWritrtracelistener class specifies the target for this class.

Some possible output goals include:

Use the System.Console.Out property to specify the Console window as the output target. Specify text files (.txt) as output targets using system.io.file.createtext ("filename.txt") statement. create

After the TextWritertracelistener object, you must add this object to

Debug.Listeners collection can receive debugging output.

Back to top

Create an example using the Debug class

Start Visual Studio .NET. Newly built a new Visual C # .NET console application project called CONINFO. Class1 will be created. Add the following namespace to the top of Class1. Using system.diagnostics; To initialize the variable to include the product's related information, add the following declaration statement to the main method: string sprodname = "widget"; int initqty = 100;

Double dunitcost = 1.03; specify the message generated as the first input parameter of the WriteLine method. Press CTRL Alt O combination to ensure that the "Output" window is visible. Debug.writeline ("Debug Information-Product Starting"); For clarity, use the Indent method to indent the back message in the Output window: Debug.indent (); To display the contents of the selected variable, please use WriteLine method as follows: Debug.writeline ("The Product Name IS" SPRODNAME);

Debug.writeline ("The Available Units On Hand Are" IUnitqty.toString ());

Debug.writeline ("The Per Unit Cost IS" DunitCost.toString ()); You can also use the WriteLine method to display the namespaces and class names of the existing object. For example, the following code displays system.xml.xmldocument namespace in the Output window: system.xml.xmldocument oxml = new system.xml.xmldocument ();

Debug.writeline (OXML); To organize the output, a category can include a second optional input parameter for the WriteLine method. If you specify a category, the Format of the Output window message is "Category: Message". For example, the first row of the following code Displays "Field: The Product Name is Widget" in the Output window: Debug.writeline ("The Product Name IS" SprodName, "Field");

Debug.writeline ("The Units On Hand Are" IUnitqty, "Field");

Debug.writeline ("The Per Unit Cost IS" Dunitcost.toString (), "Field");

Debug.writeline ("Total Cost IS" "CALC"); "Output" window can display a message when the specified condition is calculated as true when using the WriteLineIF method using the Debug class. The condition to be calculated is the first input parameter of the WriteLineIF method. The second parameter of WriteLineIf is a message that is only displayed only in the condition of the first parameter. Debug.writelineif (IUnitqty> 50, "this message will appear";

Debug.writelineif (IUnitqty <50, "this message will not a APPEAR");

Using the Assert method of the Debug class, the "Output" window is only displayed in false at the specified condition: Debug.Assert (Dunitcost> 1, "Message Will Not Appear"); debug.assert (Dunitcost <1, "Message Will Appear Since Dunitcost <1 is false ");

Create a TextWritRaceListener object for a text file called Output.txt (TR1) and named Output.txt (TR2), then add each object to the debug listener collection: TextWritertracelistener Tr1 = New TextwritertraceListener (System.Console.out);

Debug.Listeners.Add (TR1);

TextWritertracelistener Tr2 = New Textwritertracelistener (System.IO.File.Createtext ("Output.txt");

Debug.Listeners.Add (TR2); For clarity readiness, use the un mydenT method to remove the DEBUG class as the indentation generated by subsequent messages. When you use the INDENT and UninDent methods, the reader can divide the output into a group. Debug.unundent ();

Debug.writeline ("Debug Information-Product Ending"; To ensure that each Listener object receives all of its output, please call the DEBUG class buffer: debug.flush ();

Back to top

You can also use the Trace class

The Trace class generates the message that monitors the application execution.

TRACE and

DEBUG class shares most of the same way to generate output, including:

WriteLine WriteLineif Indent Unundent Assert Flush You can use or use in the same application

TRACE and

Debug class. In a "debug solution configuration" project,

TRACE and

Two outputs of DEBUG are active. The project is from these two classes.

The Listener object generates an output. However, the "Release Solution Configuration" project is only from

Trace class generates output. The "Release Solution Configuration" project ignores any

DEBUG class method call.

Trace.writeline ("Trace Information-Product Starting);

TRACE.INDENT ();

Trace.writeLine ("The Product Name IS" SPRODNAME);

Trace.writeline ("The Product Name IS" SprodName, "Field");

TRACE.WRITELINEIF (IUNITQTY> 50, "this message will appear";

Trace.assert (Dunitcost> 1, "Message Will Not Appear");

Trace.unundent ();

Trace.writeline ("Trace Information-Product Ending";

TRACE.FLUSH ();

Console.readline ();

Back to top

Confirm that it can be used

Make sure DEBUG is the current solution configuration. If the Solution Explorer window is invisible, press the Ctrl Alt L key button to display this window. Right-click "Coninfo" and click Properties. In the left pane of the Conince, under the Configuration folder, make sure the arrow points to "debug". In the Configuration folder, Configure the drop-down list box, click "Activity (Debug)" or "Debug", and then click OK. Press CTRL Alt O to display the Output window. Press the F5 key to run the code. When the "Assembly Failure" dialog appears, click "ignore". In the Console window, press ENTER. At this time, the program has been completed, the "Output" window should display the following output: debug information-product startingthe 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, TRACE INFORMATION

The Console window and 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, TRACE INFORMATION

Note: Output.txt files are located in the same directory with the CONINFO executable (Coninfo.exe). Typically, the directory is a / bin folder that stores a project source, by default, C: / Documents and settings / user login / my documents / Visual Studio Projects / Coninfo / Bin.

Back to top

Complete code list

Using system;

USING SYSTEM.DIAGNOSTICS; Class Class1

{

[Stathread]

Static void main (string [] args)

{

String sprodname = "widget";

INT IUNITQTY = 100;

Double dunitcost = 1.03;

Debug.writeline ("Debug Information-Product Starting);

Debug.indent ();

Debug.writeline ("The Product Name IS" SPRODNAME);

Debug.writeline ("The Available Units On Hand Are" IUnitqty.toString ());

Debug.writeLine ("The Per Unit Cost IS" DunitCost.toString ());

System.xml.xmldocument oxml = new system.xml.xmldocument ();

Debug.writeLine (OXML);

Debug.writeline ("The Product Name IS" SprodName, "Field");

Debug.writeline ("The Units On Hand Are" IUnitqty, "Field");

Debug.writeline ("The Per Unit Cost IS" Dunitcost.toString (), "Field");

Debug.writeline ("Total Cost IS" (IUNITQTY * DUNITCOST, "CALC");

Debug.writelineif (IUnitqty> 50, "this message will appear";

Debug.writelineif (IUnitqty <50, "this message will not a APPEAR");

Debug.assert (Dunitcost> 1, "Message Will Not Appear");

Debug.assert (Dunitcost <1, "Message Will Appear Since Dunitcost <1 is false");

TextWritertracelistener Tr1 = New Textwritertracelistener (System.Console.out);

Debug.Listeners.Add (TR1);

TextWritertracelistener Tr2 = New Textwritertracelistener (System.IO.File.Createtext ("Output.txt");

Debug.Listeners.Add (TR2);

Debug.writeline ("The Product Name IS" SPRODNAME);

Debug.writeline ("The Available Units On Hand Are" iUnitQty);

Debug.writeline ("The Per Unit Cost IS" Dunitcost;

Debug.unundent ();

Debug.writeline ("Debug Information-Product Ending"; debug.flush ();

Trace.writeline ("Trace Information-Product Starting);

TRACE.INDENT ();

Trace.writeLine ("The Product Name IS" SPRODNAME);

Trace.writeline ("The Product Name IS" SprodName, "Field");

TRACE.WRITELINEIF (IUNITQTY> 50, "this message will appear";

Trace.assert (Dunitcost> 1, "Message Will Not Appear");

Trace.unundent ();

Trace.writeline ("Trace Information-Product Ending";

TRACE.FLUSH ();

Console.readline ();

}

}

Note: To make this code example, you must pass

Using system.diagnostics; paste to the first line of code to add

System.Diagonstics namespace.

Back to top

Troubleshooting

If the solution configuration type is Release, the DEBUG class output is ignored. After creating a TextWritrtraceListener class for a specific target, TextWritRaceListener receives the output from the Trace class and the Debug class. This happens regardless of whether you use the TRACE or DEBUG class Add method to the Listeners class. If you add the Listener object of the same goal to the TRACE class and the Debug class, both the outputs will appear repeatedly in the DEBUG class or the TRACE class generated output. TextWritrtracelistener MyWriter = New TextWritertracelistener (System.Console.out);

Debug.Listener.Add (MyWriter);

TextWritertracelistener mycreator = new textwritertracelistener (system.console.out);

Trace.Listeners.Add (mycreator);

Back to top

Refer to more information, see the following topics in the .NET Framework class library document:

Trace class http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsTraceClassTopic.asp Debug class http://msdn.microsoft.com/library/default. ASP? URL = / library / en-us / cpref / html / frsrfsystemdiagnosticsdebugclasstopic.asp

For more information on tracking, see the following topics in the Microsoft GotdotNet Quick Start Tutorial:

How to use tracking? http://samples.gotdotnet.com/quickstart/howto/doc/trace.aspx

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2002) Microsoft Visual C # .NET (2003) Recent Updated: 2003-9-15 (1.2) Keywords: kbprogramming kbdebug kbnamespace kbhowtomaster KB815788 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108913.html

New Post(0)