Debugging and tracking are actually very common. The methods in the Debug class have the same name, and these methods implement the debugging functionality. The difference is that it is prohibited in the release version configuration (which means that the code cannot be invoked by the binary code). Debug output can also be set in profile settings, see below:
<confuration>
<system.diagnostics>
<debug AutoFlush = "true" Indentsize = "7"/>
</system.diagnostics>
</confuration>
Note: Debugging declarations are similar to syntax and tracking. The difference is to replace the trace with the debug
Set DEBUG Switch
The final topic of discussion is switch. switch is the object of some state. You can change the state when you configure the file or programmatically. The switch lets you create configurable debug trace code. The best way to understand the switch is to write a simple piece of code, as follows:
Using System;
Using System.Diagnostics;
Namespace switching
{
Class SampleClass
{
Create a Switch. It is initialized by a externally specified value
static TraceSwitch Generalswitch = new TraceSwitch ("Coolswitch", "Global Scope");
static public void SampleMethod ()
{
The ' The ' is written if the ' switch is ' set to TraceError
if (generalswitch.traceerror)
Console. WriteLine ("TraceError message");
The second message was written if the switch is set to Traceverbose
if (generalswitch.traceverbose)
Console.WriteLine ("Traceverbose message");
The third message was writeen if the switch is set to Tracewarning
if (generalswitch.tracewarning)
Console.WriteLine ("Treacewarning message");
The fourth message was written if the switch is set to Traceinfo
if (generalswitch.traceinfo)
Console.WriteLine ("Traceinfo message");
}
public static void Main (string[] args)
{
Calls the SampleMethod method
SampleMethod ();
}
}
}
There are several switch classes: TraceSwitch and BooleanSwitch. In this example we use TraceSwitch to create output information in accordance with their state. The switch state is checked by the Traceerrror,traceinfo,traceverbose and Tracewarning properties. These properties check the switch state and return TRUE if the trace level is equal to or greater than the corresponding constant. For example, when this level is 2 or greater then tracewarning is true, the following table is the return value:
Traceerroe
1
Tracewarning
2
Traceinfo
3
Traceverbose
4
But, as we've already said, the switch's state can be modified in code to make an example of modifying the code:
Using System;
Using System.Diagnostics;
Namespace switching
{
Class SampleClass
{
Create a Switch. It is initialized by a externally specified value
static TraceSwitch Generalswitch = new TraceSwitch ("Coolswitch", "Global Scope");
static public void SampleMethod ()
{
The ' The ' is written if the ' switch is ' set to TraceError
if (generalswitch.traceerror)
Console. WriteLine ("TraceError message");
The second message was written if the switch is set to Traceverbose
if (generalswitch.traceverbose)
Console.WriteLine ("Traceverbose message");
The third message was writeen if the switch is set to Tracewarning
if (generalswitch.tracewarning)
Console.WriteLine ("Treacewarning message");
The fourth message was written if the switch is set to Traceinfo
if (generalswitch.traceinfo)
Console.WriteLine ("Traceinfo message");
}
public static void Main (string[] args)
{
Console.WriteLine ("Before manual level set\n");
SampleMethod ();
Generalswitch.level = tracelevel.warning;
SampleMethod ();
}
}
Run the program, which contains the following information:
Before manual level set
TraceError message
Tracewarning message
Traceinfo message
After manual level set
TraceError message
Tracewarning message
These show changes to the trace switch hierarchy.
Computing performance
In this section we will tell you the time spent in debugging. In fact, debugging does not work for business logic. But it takes time to debug your code. We will calculate how long it takes to output information in the application. Measurement is important when you are testing a time when you are building a demanding application. Look at the following code:
The program tests 2 to 1000 integers and the output primes. The purpose of debugging is to test each output number, regardless of whether it is prime. If the number is not prime, then the output is failed.
Compare and measure the time with debugging and without debugging:
1
2
3
With debugging function (HH:MM:SS.FF)
00:00:07.9714624
00:00:07.9414192
00:00:07.9714624
You can see that debugging is expensive--an example that takes 64% of execution time
Conclusion:
The article describes the general method of debugging a trace. NET program. Of course there are some other problems, such as, conditional compilation we did not do. Want to learn more things, you can see MSDN. We hope this article will help you master the techniques for debugging and tracking. NET programs.
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.