There is a System.Diagnostics namespace in the. NET class library that provides a library of classes that interact with system processes, event logs, and performance counters. This 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, designed to improve the development efficiency of a broad range of developers.
Use the Debug class to help debug
The debugger is a potluck for every programmer. But we will often encounter some situations that make us headache, for example:
When we develop an interface control, the simple setting of breakpoints increases the response times of paint events and changes the environment parameters.
The breakpoint is set much, the program often stops in the normal running place, thus, debugging an error will take 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 print the information we care about in the visual Studio IDE's output window by calling the Debug.WriteLine (String message) function. You can also use Debug.Assert (bool condition) to stop the program from being in the wrong place and to display the call stack.
Calls to all functions in the Debug class are not valid in release versions. In other words, the code we add through this method can be used only for debugging purposes, and without deleting any code at the time of release, we can give the user a program without debugging instructions.
The following example illustrates these two functions to help debug the method:
1. Create a new visual Studio C # project with the default project name.
2, drag a label to the Form1 and use its default ID.
3, add the following function code to the Form1 class in Form1.cs:
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 Output window.
6, switch to just the program, change the size of the main window, you can see the Form1 window in the IDE real-time size, and on the Form1 see the number of OnPaint called. When the width of the window is less than 200 pixels, the system pops up a assertion Fail dialog box. The call Stack for the current program is shown inside. If you set a breakpoint in OnPaint and want to debug the program, you go into 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 two classes with similar features?
The reason for this is that the functions provided in the Debug class only work with #debug macro parameters at compile time, and these functions are ignored once they are in release version. That is, the functionality of the Debug class is only available when the programmer is developing it. And trace is different, it can be in release version of the program is also run, so that programmers can in release version of the program to add some of the Debug class provides functionality.
Use the Trace class to do the program log
The next question is: what can we programmers do with the functionality of the Trace class? We can use it to do the log of the program.
1, open just project.
2, with the following code to cover the 2nd step of the code:
3, add the following code at the end of the constructor Form1 () 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, add the following code in the Button1_Click function:
Calculate ();
Application.exit ();
5, run the release version of the program, click on the added button, the program will begin to perform a random number division. Because it is a random number, it is possible to have several 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 Records of each moment, and the exception record in the log.
Summary
Using the System.Diagnostics.Debug class and the System.Diagnostics.Trace class can help programmers easily debug programs and detect program performance.
All calls to the Debug class are valid only in the debug version of the program, and the Trace class invocation works in release and debug versions.
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.