Debugging and Tracing in ASP.NET (4/4)
[previous] |
Debugging and Tracing in ASP.NET
Tracing
The most important class for tracing is System.Diagostics.Trace
. The
Trace
class has the same methods and properties as the Debug
class
except that its methods have the Conditional
attribute of TRACE
. In
Visual Studio .NET TRACE
is defined in both debug and release builds by default.
So the methods of the Trace
class are always executed by default. So this
class should be used only when we always want the output.
As with Debug
, trace output goes to all listeners that are registered with
the Trace
class. The same listeners that are used for Debug
can be
used for Trace
.
The only other difference is that while Debug
has a binary nature of either
being on or off, Trace
should be used in a multilevel hierarchy. To do this a
switch called TraceSwitch
is defined. This is similar to the
BooleanSwitch
class except that it has a level
property that can
have five values defined in the TraceLevel
enumeration. Combined with the
TraceSwitch
class Trace
can be used to provide flexible tracing
of applications as shown below.
// Create a TraceSwitch.
static TraceSwitch traceSwitch = new TraceSwitch("trace", "Application");
static public void TraceOutput()
{
// Verbose Tracing.
if(traceSwitch.TraceVerbose)
{
Trace.Write("Trace Output - Verbose");
}
// Error Tracing
if(traceSwitch.TraceError)
{
Trace.WriteLine("Trace Output - Error");
}
}
As always, though the switches, levels and listeners can be defined in code, they should be defined in the application's config file so that they can be changed whenever necessary without recompilation.
Conclusion
The System.Diagnostics
namespace provides useful classes for debugging
applications. These become important specifically while writing server side applications
where IDE debuggers are difficult to use. These classes provide extensive debugging,
tracing, event logging and performance monitoring features.
About the Author
Utpal Chakraborty is Manager, Software Engineering at Organic (https://www.organic.com). He has extensive experience in developing enterprise applications using Microsoft and non-Microsoft technologies. He can be reached at [email protected].
[previous] |
Created: December 4, 2002
Revised: December 4, 2002
URL: https://webreference.com/programming/asp/debugging/4.html