Monday, May 25, 2009

Programmatically Debugging / detecting Defects using C# Programs

This methodology can be used when the result of a defect is known but it is not known how to routinely reproduce the issue

Its hard to detect defects when we do not know the routine in which it occurs. In simpler terms, if we do not know the sequence and order precedence for occurrence of a defect or bug, then it will be difficult to reach the code to fix the defect.

Though Visual Studio provides excellent debugging environment, at times it will be difficult for developers to detect the defects. For those cases and to reproduce the exact set of events under which the abnormality is occurring. Solving the issues usually requires additional information. This is where the System.Diagnostics Debug and Debugger classes are tools you can use to provide targeted information.

Using System.Diagnostics.Debug

The System.Diagnostics.Debug class provides method calls and properties that can provide formatted conditional information. Code containing the Debug class can remain in release code as it is not compiled into IL unless the DEBUG compilation attribute is set

Useful Methods in this class are

1. Assert
2. Equals
3. Indent
4. Unindent
5. WriteLineIf


example: Debug.Assert(inputValue < Byte.MaxValue, "Value is out of range");

Using System.Diagnostics.Debugger

The System.Diagnostics.Debugger class communicates with an attached debugger. When an assembly is executed within an attached debugger, the Debugger.Break method will stop execution as if a breakpoint is hit by the IDE.

No comments:

Post a Comment