Usage of the debug and trace classes in the system. Diagnostics namespace

Source: Internet
Author: User
Document directory
  • Usage of the debug and trace classes in the system. Diagnostics namespace
Usage of the debug and trace classes in the system. Diagnostics namespace Summary
There is a system. Diagnostics namespace in the. NET class library, which provides some class libraries that interact with system processes, event logs, and performance counters. It includes two very useful classes for developers-Debug and trace. This article introduces some basic functions of these two classes to improve the development efficiency of developers.

--------------------------------------------------------------------------------

Directory
Debug class
Differences between the debug class and the trace class
Use the trace class for program logs
Summary
--------------------------------------------------------------------------------

Debug class
Debugging programs are common to every programmer. However, we often encounter some problems, such:
When we develop an interface control, a simple breakpoint will increase the response times of the paint event, resulting in changes to the environment parameters.
When there are too many breakpoints, the program often stops at the place where it runs normally. In this way, debugging an error takes a lot of time to find the error.
At this time, we need to use the system. Diagnostics. debug class to help us debug. We can print the information we care about in the output window of Visual Studio ide by calling the debug. writeline (string message) function. You can also use DEBUG. Assert (bool condition) to stop the program in the wrong place and display the call stack.
All functions in the debug class are not called in the release version. That is to say, the code we add through this method can only be used for debugging. When releasing the code, you can give the user a program without debugging instructions without deleting any code.
The following example demonstrates how to debug these two functions:
1. Create a New Visual Studio C # project with the default project name.
2. Drag a label to form1 and use its default ID.
3. Add the following function code to the form1 class in form1.cs:
Private int time = 0;
Protected override void onpaint (painteventargs E)
{
Time ++;
This. label1.text = "onpain called" + time. tostring () + "Times .";
}

Protected override void onresize (eventargs E)
{
System. Diagnostics. Debug. Assert (this. width> 200, "width shocould be larger than 200 .");
System. Diagnostics. Debug. writeline (size. tostring ());
}

4. Compile and run the debug version of the project.
5. Switch Visual Studio. NET ide To the output window.
6. Switch to the program just now and change the size of the main window. You can see the real-time size of the form1 window in IDE and the number of onpaint calls on form1. When the window width is less than or equal to 200 pixels, an assertion fail dialog box is displayed. It shows the call stack of the current program. If you set a breakpoint in onpaint and want to debug the program, you will enter an endless loop until you stop debugging.

--------------------------------------------------------------------------------

Differences between the debug class and the trace class
You must have discovered that there is a class named trace in the system. Diagnostics namespace. Its functions are very similar to those of debug. Why are there two similar functions?
The reason is that the functions provided in the debug class only work when the # debug macro parameter is included in the compilation. Once the function is in the release version, these functions will be ignored. That is to say, the debug class function can only be used during development by programmers. Trace is different. It can also be run in the release version program, so that programmers can add some debug class functions in the release version program.

--------------------------------------------------------------------------------

Use the trace class for program logs
The next question is: What can our programmers do with the trace function? We can use it for program logs.
1. Open the project.
2. Use the following code to overwrite the code in Step 1:
Private void calculate ()
{
Int A = 1, B = 1;
Try
{
System. Random r = new random ();
While (true)
{
A = (INT) (R. nextdouble () * 10 );
B = (INT) (R. nextdouble () * 10 );
System. Diagnostics. Trace. writeline (system. datetime. Now. tostring () + ":" +
A. tostring () + "/" + B. tostring () + "=" + (a/B). tostring ());
}
}
Catch (exception ex)
{
System. Diagnostics. Trace. writeline (system. datetime. Now. tostring () + ":" + A. tostring () +
"/" + B. tostring () + "=" + "error:" + ex. Message );
MessageBox. Show (ex. Message );
}
}

3. Add the following code at the end of the constructor form1 () to redirect the trace output to the app. log file:
System. Diagnostics. Trace. listeners. Clear ();
System. Diagnostics. Trace. autoflush = true;
System. Diagnostics. Trace. listeners. Add (new system. Diagnostics. textwritertracelistener ("app. log "));

4. Drag a button to the form, double-click the button, and add the following code to the button#click function:
Calculate ();
Application. Exit ();

5. Run the release version of the program. Click the Add button to execute a random number division. Because it is a random number, the number may be 0, so that the program will throw an exception, which is automatically aborted by the program.
6. In the directory where the program is located, you can find a new file app. log, which records the operation records at various times and records the exception records in the log.

--------------------------------------------------------------------------------

Summary
The system. Diagnostics. debug class and system. Diagnostics. Trace class can help programmers easily Debug Programs and detect program running conditions.
All calls of the debug class are only valid in the debug version of the program, while calls of the trace class are valid in both the release and DEBUG Versions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.