This article describes how to use the debug and trace classes. Microsoft. NET Framework provides these two classes. You can use these two classes to provide information about application performance during application development or after deployment to the product. These two classes are only part of the configuration functions provided in. NET Framework.
The following lists the recommended hardware, software, network structure, and required Service Packs:
• Microsoft Windows 2000 or Microsoft Windows XP
• Microsoft Visual C #. net
This article also assumes that you are familiar with program debugging.
Method description
The steps described in the example section for creating a debug class demonstrate how to create a console application that uses the debug class to provide information about program execution.
When the program runs, you can use the debug method to generate messages to help you monitor the program execution sequence, detect faults, or provide performance metrics. By default, messages generated by the debug class are displayed in the "output" Window of the integrated development environment (IDE) of Visual Studio.
This code example uses the writeline method to generate a message with a line terminator. When you use this method to generate a message, each message is displayed as a separate line in the "output" window.
If the assert method of the debug class is used, the message is displayed in the "output" window only when the specified condition is calculated as false. The message is displayed in a mode dialog box. The dialog box contains the message, project name, and Debug. Assert statement number. The dialog box also contains the following command buttons:
• Stop: The application stops running.
• Retry: The application enters the debugging mode.
• Ignore: the application continues.
You must click one of these buttons before the application can continue.
You can also specify to output from the debug class to a target other than the "output" window. The debug class has a set named listeners, which includes some listener objects.
Each listener object monitors the debug output and directs the output to the specified target.
Each listener in the listener set receives any output generated by the debug class. Use the textwritertracelistener class to define the listener object. You can use the constructor of the textwritertracelistener class to specify a target for this class.
Some possible output targets include: • Use the system. Console. Out attribute to specify the "console" window as the output target.
• Use the system. Io. file. createtext ("filename.txt") statement to specify the text file (.txt) as the output target.
After creating a textwritertracelistener object, you must add the object to the Debug. Listeners collection to receive debugging output.
Use the debug class to create an example 1. Start Visual Studio. NET.
2. Create a New Visual C #. Net console application project named coninfo. Class1 will be created.
3. Add the following namespace at the top of class1.
Using system. diagnostics;
4. to initialize a variable to include information about the product, add the following statement to the main method:
String sprodname = "widget ";
Int iunitqty = 100;
Double dunitcost = 1.03;
5. Specify the message generated by the class as the first input parameter of the writeline method. Press CTRL + ALT + O to make sure the "output" window is visible.
Debug. writeline ("debug information-product starting ");
6. To be clear and easy to read, use the indent method to indent the following message in the "output" window:
Debug. indent ();
7. To display the content of the selected variable, use the 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 ());
8. You can also use the writeline method to display the namespace and class name of an existing object. For example, the following code shows the system. xml. xmldocument namespace in the "output" window:
System. xml. xmldocument oxml = new system. xml. xmldocument ();
Debug. writeline (oxml );
9. to sort out the output, you can include a category as the second optional input parameter of the writeline method. If you specify a category, the format of the "output" window message is "category: Message ". For example, the first line 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" + (iunitqty * dunitcost), "calc ");
10. The "output" window displays messages only when the specified condition is calculated as true using the writelineif method of the debug class. The condition to be calculated is the first input parameter of the writelineif method. The second parameter of writelineif is displayed only when the condition of the first parameter is calculated as true.
Debug. writelineif (iunitqty> 50, "This message will appear ");
Debug. writelineif (iunitqty <50, "This message will not appear ");
11. Use the assert method of the debug class to make the "output" window display messages only when the specified condition is calculated as false:
Debug. Assert (dunitcost> 1, "message will not appear ");
Debug. Assert (dunitcost <1, "message will appear since dunitcost <1 is false ");
12. Create a textwritertracelistener object for the text files in the "console" window (tr1) and named output.txt (tr2), and add each object to the debug listeners set:
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 );
13. To be clear and easy to read, use the unindent method to remove the indentation generated by the debug class for subsequent messages. When you use indent and unindent together, the reader can divide the output into groups.
Debug. unindent ();
Debug. writeline ("debug information-product ending ");
14. To ensure that each listener object receives all of its output, call the flush method for the debug buffer class:
Debug. Flush ();
You can also use the trace class to generate messages that monitor application execution. The trace and debug classes share most of the same methods to generate output, including: • writeline
• Writelineif
• Indent
• Unindent
• Assert
• Flush
You can use trace and debug classes in the same application or simultaneously. In a "Debug solution configuration" project, both trace and debug outputs are active. This project generates output from these two classes for the listener object. However, the release solution configuration project only generates output from the trace class. This release solution configuration project ignores any debug class method calls.
-Xiaotiandi-blog Park
Http://www.cnblogs.com/beartohxj/archive/2009/06/14/1503208.html