How Do I...Use tracing switches and listeners?
Trace instrumentation allows developers and administrators
to monitor the health of applications running in real-life settings (as opposed to
running in a debugger). Sometimes using a debugger can hide bugs and
obscure some performance and threading problems. Tracing is a very important
monitoring and debugging tool for distributed, multitier applications. Such
applications often contain problems that you can only observe when the application is
under a heavy load, and in the inherent randomness of a real-life environment.
This sample illustrates the basics of using the trace and
Boolean switches. A switch provides an efficient mechanism for
enabling and disabling code paths at runtime using external settings. After running it, take a look at the source code. You will see how easy it is to add simple tracing to your applications. You can then go to the config file and
modify the settings of the switches and then run the sample again. You will
possibly get a completely different output.
Set value property of the Boolean switch to one of the
following: 0 (false) or 1 (true)
Set value property of the trace switch to one of the
following: 0 (off), 1 (error), 2 (warning), 3 (info), or 4 (verbose):
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="ABooleanSwitch" value="1" />
<add name="ATraceSwitch" value="4" />
</switches>
<trace autoflush="false" indentsize="4" />
</system.diagnostics>
</configuration>
For more information about switches, please refer to the SDK documentation.
You have to compile your instrumented applications with
trace or debug compiler directives enabled. Otherwise, all calls to Trace or
Debug (respectively) are ignored during compilation.
Before running the application, open a command line window
and run the Debug Monitor utility (DBMon.exe). You will be able to see all debug and trace messages coming
from any application running on your system.
Creating new event log sources requires administrative rights. Since this sample creates "SwitchesDemo" source, you need to run it under an administrative account.
Note: DBMon.exe is available from the Windows Platform SDK on the Microsoft Developer Network website.
Example
C# Switches.exe
[This sample can be found at M:\web\users\sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Services\Tracing\Switches\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|