Debugging and Tracing in ASP.NET (2/4) | WebReference

Debugging and Tracing in ASP.NET (2/4)

To page 1current pageTo page 3To page 4
[previous] [next]

Debugging and Tracing in ASP.NET

Debugging Helper Classes

These classes provide the basic debugging features. The following sections describe each of these classes in detail.

System.Diagonostics.ConditionalAttribute

Attaching metadata to code using attributes is a great feature in .NET. We can use this feature to write code that is conditionally compiled and invoked during program execution. Earlier, this was done in C/C++ using #ifdef statements. While this method can still be used in C# it leads to unclean code since the code gets sprinkled with preprocessor directives. The Java way of doing things is slightly better than C/C++. In Java, debugging code is put within an if...else block as shown below.

public static boolean debug = true;
if(debug)
{
	MyDebugMethod(); // debugging action here...
}

The Java compiler is usually smart enough not to compile the call to the debugging method and not include it in the byte code when the debug variable is set to false. However, all that has been accomplished is the replacement of a preprocessor directive of C/C++ with an if statement. Not a big change.

The System.Diagonostics.ConditionalAttribute attribute provides a much cleaner way of writing debugging code. Once methods that are exclusively used for debugging have been defined they are marked with the conditional attribute as shown below.

[Conditional("DEBUG")]
public void MyDebugMethod ()
{
	// debugging code here...
}

When the debug method needs to be called it can be called just like any other method. The call is compiled by the C# compiler only if the compiler directive DEBUG has been defined. This entirely localizes the debugging nature of the method instead of the code globally being aware of it. Thus, the code that uses this debugging method needs to do no special handling (like preprocessor directives or an if statement). Usually, in Visual Studio .NET DEBUG and TRACE are both defined when the debug build configuration is chosen. If it is necessary to define these explicitly it can always be done by including the following statement at the top of any file.

#define DEBUG

System.Diagonostics.BooleanSwitch

The use of conditional attributes is fine when we need to do debugging during application development. However, it does not allow us to turn debugging on or off during execution time. To have such dynamic control we need to read in some boolean value at run time and have our debugging methods called depending on the variable. While this can be custom coded very simply, .NET provides a class, System.Diagonostics.BooleanSwitch, specifically tailored for reading boolean variables from configuration files. In the case of ASP.NET the web.config file of the application should contain the definition and the value of the switch. An example where a switch named debugSwitch is defined is shown below. (In the case of stand alone applications this switch should be defined in the applications configuration file.)

<configuration>
 	
	...
    <system.diagnostics>
       	<switches>
          <add name="DebugSwitch" value="1" />
        </switches>
    </system.diagnostics>
</configuration>

Once the switch has been defined in the configuration it can be declared in the application as follows.

public static BooleanSwitch debug = 
   new BooleanSwitch("DebugSwitch", "Debug Switch");
It is a good idea to make these switches static so that they are read in only once. After they are declared debugging code can be written as shown below:
if(debug.Enabled)
{
	// debugging action here...
}

In this way debugging can be turned on and off without any recompilation. The only slight penalty is that a boolean check is always performed irrespective of whether debugging is on or off.


To page 1current pageTo page 3To page 4
[previous] [next]

Created: December 4, 2002
Revised: December 4, 2002

URL: https://webreference.com/programming/asp/debugging/2.html