debugging
In fact, debugging and tracking is used very common. The method in the DEBUG class has the same name, these methods implements debugging features. The difference is that it is forbidden to use in the release version (this means that the binary code calls these code). The debug output can also be set in the configuration file, please see below:
confuration>
Remarks: The debug declaration and syntax and tracking are very similar. Different, it is to replace TRACE places to Debug
Set debug switch
The theme of the final discussion is Switch. Switch is an object with some states. You can change the status when the configuration file or program is programmed. Switch lets you create configurable debug tracking code. It is best to understand the Switch method is to write a simple code as follows:
Using system;
Using system.diagnostics;
Namespace Switching
{
Class SampleClass
{
// Create a switch. It is initialized by an externally specified value
Static TraceSwitch Generalswitch = New Traceswitch ("Coolswitch", "Global Scope");
Static Public Void SampleMethod ()
{
// the first message is written if the switch state is set to travelerror
IF (generalswitch.tracerror)
Console.WriteLine ("TraceError Message);
// the second message is written if the switch state is set to trackbose
IF (generalswitch.traceverbose)
Console.writeline ("TraceverBose Message";
// the third message is writeen if the switch state is set to traceWarning
IF (generalswitch.tracewarning)
Console.writeline ("TreaceWarning Message);
// the fours message is written if the switch state is set to trackinfo
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 status. The Switch status is checked by TraceerRrror, TraceInfo, TraceverBose and TraceWarning property. These attributes check the Switch state and if the TRACE level is equal to or greater than the corresponding constant, then TRUE will be returned. For example, when this level is 2 or more, TRACEWARNING is TRUE, the table below is the return value:
Traceerroe
1
TraceWarning2
Traceinfo
3
TRACEVERBOSE
4
However, as we have already said, Switch's status can be modified in the code and make an example of modifying the code:
Using system;
Using system.diagnostics;
Namespace Switching
{
Class SampleClass
{
// Create a switch. It is initialized by an externally specified value
Static TraceSwitch Generalswitch = New Traceswitch ("Coolswitch", "Global Scope");
Static Public Void SampleMethod ()
{
// the first message is written if the switch state is set to travelerror
IF (generalswitch.tracerror)
Console.WriteLine ("TraceError Message);
// the second message is written if the switch state is set to trackbose
IF (generalswitch.traceverbose)
Console.writeline ("TraceverBose Message";
// the third message is writeen if the switch state is set to traceWarning
IF (generalswitch.tracewarning)
Console.writeline ("TreaceWarning Message);
// the fours message is written if the switch state is set to trackinfo
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, contain the following information:
Before Manual Level Set
TraceError Message
TraceWarning Message
Traceinfo Message
After Manual Level Set
TraceError Message
TraceWarning Message
These showcases the TRACE SWITCH level.
Computational performance
This part we will tell you the time of debugging. In fact, debugging does not work for business logic. But the debug code takes time. We will calculate the time of output information in the application. When you test one is a strict application time, measurement is important. Look at the following code:
Using system;
Using system.diagnostics;
Namespace DebugDemo
{
Class PrimberDetector
{
Public Static Bool Isprime (int N)
{
INT UPPERBOUND = (int) math.sqrt (n);
For (int i = 2; i <= Upperbound; i )
{
Debug.writeline ("Processing Number" N ", Testing with" i); if ((N% i) == 0)
{
Debug.writeline ("failed");
Return False;
}
}
}
Public Application
{
[Stathread]
Static void main (string [] args)
{
For (int i = 2; i <10000; i )
PRIMENUMBERDETOR.ISprime (i))
Console.WriteLine ("{0} is prime number", i);
}
}
}
The program tests 2 to 1000 integers and output prime numbers. The purpose of debug is to test each output number, whether it is a prime number. If the number is not a prime number, then output Failed.
Contrast measurement with debugging and non-commissioned time:
1
2
3
Toned test function (HH: mm: ss.ff)
00: 00: 07.9714624
00: 00: 07.9414192
00: 00: 07.9714624
Without debugging
(hh: mm: ss.ff)
00: 00: 05.1273728
00: 00: 05.5179344
00: 00: 05.1273728
It can be seen that debugging is expensive - spending 64% execution time in the example
in conclusion:
The general method of debugging tracking .NET program is described in the article. Of course, there are some other issues, such as, if you don't do it. I want to learn more, I can see MSDN. We hope this article to help you master the technique of debugging .NET programs.