The difference between the Debug class and the Trace class

Source: Internet
Author: User

There is a system.diagnostics in the. NET class Library
namespace, which provides some class libraries that interact with system processes, event logs, and performance counters. It includes two class--debug classes and trace classes that are useful to developers. This article describes some of the basic uses of these two classes, which are designed to improve the development efficiency of a broad range of developers.

Use the Debug class to help debug

Debugging is a routine for every programmer. But we often encounter situations that make us headache, such as:


When we develop an interface control, a simple breakpoint will increase the response times of the paint event, and the environment parameter changes.

Breakpoints are set more frequently, and programs often stop where they normally work, so debugging an error takes a lot of time to find the error.

At this point, we need to use the System.Diagnostics.Debug class to help us Debug. We can do this by calling Debug.WriteLine (string
message) function to print the information we care about in the Output window of the Visual Studio IDE. You can also use Debug.Assert (bool
condition) to make the program stop in the wrong place and display the call stack.


Calls to all functions in the Debug class are not valid in the release version. In other words, the code we add in this way can be used only for debugging, and without deleting any code at the time of publishing, you can give the user a program without debugging instructions.


The following example demonstrates these two functions to help debug the method:

1. Create a new Visual Studio C #
Project, with the default item name.

2. Drag a label to Form1 and take 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 should be larger
than 200. ");
System.Diagnostics.Debug.WriteLine (Size.tostring ());
}


4. Compile and run the debug version of the project.

5. Switch the Visual Studio. NET IDE to the Output window.


6.
Switch to the program you just made, change the size of the main window, you can see the real-time size of the Form1 window in the IDE, and see the number of times OnPaint is called on Form1. When the width of the window is less than or equal to 200 pixels, the system pops up a assertion
The dialog box for fail. It shows the call stack for the current program. If you set a breakpoint in OnPaint and want to debug the program, you will enter a dead loop until you stop debugging.


The difference between the Debug class and the Trace class


You must have found a class named Trace in the System.Diagnostics namespace. Its function functions are very similar to debug. Why should there be such two classes with similar functions?


The reason for this is that the functions provided in the Debug class only work with the #debug macro parameter at compile time, and once in the release version, these functions will be ignored. This means that the Debug class function is only available when the programmer is developing it. And trace is different, it can be in the release version of the program is also run, so that programmers can be in the release version of the program to add some of the features of the Debug class.


Using the trace class to do the program log

The next question is: what can our programmers do with the functionality of the Trace class? We can use it to do the log of the program.


1. Open the project you just started.

2. Use the following code to overwrite the code for the 2nd step just now:

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.
At the end of the constructor Form1 (), add the following code to redirect the output of trace 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 Button1_Click function:

Calculate ();

Application.exit ();

5.
Run the release version of the program, click the Add button, and the program starts executing a random number division. Because it is a random number, there may be a few 0 cases, so the program will throw exception, which is the program will automatically abort.


6, in the directory where the program is located you can find a new file App.log, which records the operation of the various time records, and the exception record in the log.


Summary

Using System.Diagnostics.Debug classes and System.Diagnostics.Trace
Class can help programmers easily debug programs and detect program performance.

The difference between the Debug class and the Trace class

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.