Debug is one of the most painful parts of the entire software development process. We don't want to find how difficult a small bug is - you may already know. The number of bugs in the software increases with the complexity of the software and often increasing the BUG in time. These bugs and software are responsible for each other, making engineering more complicated. So we continue to monitor and modify bugs.
The best way is to perform unit testing to correct BUG. When the software has problems, the software will not tell us where there is an error, why there is an error, our task is to track the process to solve them.
This article we just want to tell you how to use DOTNET Framework to debug and track, making this process simple. We will tell you how to use them effectively and use the combination example.
track
The strategy of debugging to be discussed in this article is tracking. Tracking is very powerful because he allows you to see the entire behavior of the application, and analyze him is the most effective, although he cannot provide information.
DOTNET provides tracking features in the System.Diagnostics name space, correctly, TRACE CLASS
TRACE is a static class (this means that all members are static, you don't need to initialize him to get its function)
. Generate an assertion (conditional or no)
. Output tracking information based on the conditions provided
. Format tracking output information
Let us start research from a brief member method:
Public static void assert (bool)
Public Static Void Assert (Bool, String)
Public Static Void Assert (Bool, String, String)
The Assert method displays a failure message (display information when the application fails, allows the user to interrupt execution, ignoring the error or re-running the error), if the condition is false, the two overload functions allow the designated custom one or two Follow the information. (This information is set by String parameter, which allows developers to display additional assertion failures)
// ASERTION DEMO
//
// purpose: To Demonstrate Results of Different Assert Method Calls
Using system;
Using system.diagnostics
Namespace assertion
{
Class Application
{
[Stathread]
Static void main (string [] args)
{
// Simple assertion. No Additional Message
Trace.assert (false);
}
}
}
The program will display the following message dialog
As you can see, just displaying exception information. No context information is displayed, so we can't see the reason for failure. Below, the application calls an ASSERT method to use its properties:
// ASERTION DEMO
//
// purpose: To Demonstrate Results of Different Assert Method Calls
Using system;
Using system.diagnostics
Namespace assertion
{
Class Application
{
[Stathread]
Static void main (string [] args)
{
// Simple assertion. No Additional Message
Trace.assert (false, "simple assertion message);
}
}
}
This program shows a dialog box for more information. It can be seen that we can provide information about the cause of failure. Call an ASSERT method for two information, which makes a more detailed assertion:
// ASERTION DEMO
//
// purpose: To Demonstrate Results of Different Assert Method Calls
Using system;
Using system.diagnostics
Namespace assertion
{
Class Application
{
[Stathread]
Static void main (string [] args)
{
// Simple assertion. No Additional Message
TRACE.ASSERT (False, "Simple Assertion Message", "This Message Just An Example.in Real Application You CAN ProvItem Detailed Information Here ');
}
}
}
You now see more detailed information:
In fact, this method is used when an important condition is checked, such as the correctness of the data. Lower is the most common example: public void storeObject (PersistentObject Obj)
{
Trace.assert (Obj! = Null, "Cannot Store Null Object");
}
This method is stored when the Object object is not NULL. When the program fails, it is the best time to produce an exception, but this is not a necessary condition. For example, if this method saves some important program data, assertion is the perfect choice.
Remarks: Debugging tracking is useful, if the final release version contains assertion information that is unbearable to the user. The following tricks can help you control the tracking switch.
If you have a manual compilation project (such as the command line compilation), the tracking information is displayed by default. If you use a tracking function in a C #, when you compile the code, add the / d: TRACE flag to compile the command line, or you can simply join the top of the #define trace to the file.
For example, the following applet:
Using system;
Using system.diagnostics;
Namespace TraceShow
{
Class Calss1
{
///
/// the main entry point for the application
/// summary>
[Stathread]
Static void main (string [] args)
{
Trace.Listener.Add (New Textwritertracelister (Console.out);
Trace.Write ("Hey, this Is A TRACE Message / N", "Simple Message";
}
}
}
If you are compiled in the command line, you will not see any information unless you add / d: trace or join #Define trace to the top of the file.
When you compile programs in Visual C #, this situation will change, and the default is available in Visual C #, the conclusion is that you will see tracking information. Turn off the debugging function, browse the project properties (you can be in the solution browser, or the View-> Property menu item. In this Properties Page dialog, the conditional compilation constant is found (under the Configuration Properties folder, this option is locked. State), remove the Trace option): Public Static Void Fail (String)
Public Static Void Fail (String, String)
The FAIL method produces an unconditional assertion. It is a bit like a ASSERT method, but he does not need any processing conditions.
Failure conditions with simple conditions cannot be selected so use. The example below is this abnormal treatment situation:
Try
{
Throw New Exception ("Sample Exception");
}
Catch (Exception EX)
{
Trace.fail ("Exception Caught", EX.MESSAGE);
}
Pubic Static Void Write (Object)
Public static void write (string)
Public Static Void Write (Object, String)
Public Static Void Write (String, String)
The Trace class can write tracking information without any conditions. For example, information is output to the device of the recipient as a tracking information. Use the WRITE method to perform the output.
The WRITE method can establish a drawing information as an object or string. In the previous case, Object.toString is executed by call. There are other three ways to perform similar behavior: WriteLine output line, WriteIF-condition information, WriteLineif Output a line of condition information.
The second parameter of the WRITE method specifies the classification before the information is written (such as a string)
The output is registered as a listener. The listener is an object that can output tracking information to some devices. Note that Assert and Fail methods often output error messages to forms or consists, regardless of that listener is selected. Such objects must inherit the TraceListener class, and he has the following important methods:
Public Virtual Void Fail (String)
Public Virtual Void Fail (String, String)
Public Virtual Void Flush (String)
Public Virtual Void Write (Object)
Public Abstract Void Write (String)
Public Virtual Void Write (Object, String)
Public Virtual Void Write (String, String)
Public Virtual Void WriteLine (Object)
Public Virtual Void WriteLine (String)
Public Virtual Void WriteLine (Object, String)
Public Virtual Void WriteLine (String, String)