Recipe8.2.Providing Fine-Grained Control over DebuggingTracing Output


Recipe 8.2. Providing Fine-Grained Control over Debugging/Tracing Output

Problem

Your application consists of multiple components. You need, at specific times, to turn on debug/trace output for a select few components, while leaving all other debug/trace output turned off. In addition, you need control over the type and amount of information that is produced by the trace/Debug statements.

Solution

Use the BooleanSwitch class with an application configuration file (*.config). The following method creates three switches for your application: one that controls tracing for database calls, one that controls tracing for UI components, and one that controls tracing for any exceptions that are thrown by the application:

 public class Traceable {     BooleanSwitch DBSwitch = null;     BooleanSwitch UISwitch = null;     BooleanSwitch exceptionSwitch = null;     [System.Diagnostics.ConditionalAttribute("TRACE")]     public void EnableTracing( )     {         DBSwitch = new BooleanSwitch("DatabaseSwitch",                    "Switch for database tracing");         Console.WriteLine("DBSwitch Enabled = " + DBSwitch.Enabled);         UISwitch = new BooleanSwitch("UISwitch",                    "Switch for user interface tracing");         Console.WriteLine("UISwitch Enabled = " + UISwitch.Enabled);         exceptionSwitch = new BooleanSwitch("ExceptionSwitch",                           "Switch for tracing thrown exceptions");         Console.WriteLine("ExceptionSwitch Enabled = " + exceptionSwitch.Enabled);     } } 

After creating each switch, the Enabled property is displayed, indicating whether the switch is on or off.

Creating these switches without an application configuration file results in every switch being disabled. To control what state each switch is set to, use an application configuration file, shown here:

 <?xml version="1.0" encoding="utf-8" ?> <configuration>     <system.diagnostics>         <switches>             <clear/>             <add name="DatabaseSwitch" value="1" />             <add name="UISwitch" value="0" />             <add name="ExceptionSwitch" value="0" />         </switches>     </system.diagnostics> </configuration> 

The TraceSwitch class can also be used with an application configuration file (AppName.exe.config). The following method creates a new TRaceSwitch object with a level assigned by the application configuration file:

 public class Traceable {     TraceSwitch DBFilterSwitch = null;     TraceSwitch UIFilterSwitch = null;     TraceSwitch exceptionFilterSwitch = null;          public void SetTracingFilter( )     {         DBFilterSwitch = new TraceSwitch("DatabaseFilter",                          "Filter database output");         Console.WriteLine("DBFilterSwitch Level = " + DBFilterSwitch.Level);         UIFilterSwitch = new TraceSwitch("UIFilter",                          "Filter user interface output");         Console.WriteLine("UIFilterSwitch Level = " + UIFilterSwitch.Level);         exceptionFilterSwitch = new TraceSwitch("ExceptionFilter",                                 "Filter exception output");         Console.WriteLine("exceptionFilterSwitch Level = "                           + exceptionFilterSwitch.Level);     } } 

After creating each filter switch, the Level property is displayed to indicate the switch's level.

Creating these switches at this point results in every switch's level being set to zero. To turn them on, use an application configuration file, shown here:

 <?xml version="1.0" encoding="utf-8" ?> <configuration>     <system.diagnostics>         <switches>             <clear/>             <add name="DatabaseFilter" value="4" />             <add name="UIFilter" value="0" />             <add name="ExceptionFilter" value="1" />         </switches>     </system.diagnostics> </configuration> 

This XML file contains a nested tag called switches. This tag defines switch names and sets a value indicating the level of the switch. The traceSwitch class accepts the five predefined trace levels shown in Table 8-1. The level of the traceSwitch can be set through code, but that defeats the flexibility of using a configuration file.

Table 8-1. The TraceSwitch class's tracing levels

Level name

Value

Default

Off

0

Yes

Error

1

No

Warning

2

No

Info

3

No

Verbose

4

No


For more information on the application configuration file, see Recipe 8.1.

Discussion

Turning tracing on or off involves the BooleanSwitch class. When the BooleanSwitch is created, it attempts to locate a switch with the same name as the displayName parameter in either the machine.config or application configuration file. If it cannot locate this name in either file, BooleanSwitch.Enabled is set to false.

The application configuration file for a WinForms-or Console-based application is an XML file named with the assembly's name followed by .exe.config. An ASP.NET-based web application can have multiple web.config files (one in each directory of the application). An application will automatically use the configuration file(s) that is (are) appropriate; however, the top-level configuration file must be in the same main directory as the application. Notice the switches tag nested inside the system. diagnostics element. This tag allows switches to be added and their values set. For Boolean switches, a zero turns the switch off, and any other positive or negative number turns it on. The Enabled property of the BooleanSwitch can be set through code or by setting the value in the config file.

This XML file must have the same name as the executable using these switches, followed by .config. For example, if the executable name were Accounting.exe, the configuration file would be named Accounting.exe.config. This file should be placed in the same directory as the executable Accounting.exe. For more information on this file, see Recipe 8.1.

The application configuration file can also set trace and debug output levels in this same switches tag. These levels identify the scope of the output, for example, if the output will contain only warnings, only errors, only informational messages, or some combination thereof. The level specified is the maximum trace level for the switch so it includes all levels below it up through that level. Of course, this is only an example; you may define your own levels as well. For more information on controlling these output levels, see Recipe 8.3.

The TraceSwitch class operates similarly to the BooleanSwitch class, except that the traceSwitch class encapsulates the available levels that control the type and amount of debug/trace output. The BooleanSwitch class is simply an on/off switch used to enable or disable debugging/tracing.

When the traceSwitch is created, it attempts to locate a switch with the same name as the displayName parameter in either the machine.config or application configuration files. If it cannot locate this name in either file, the traceSwitch.Level property is set to zero.

The application configuration file can also enable or disable trace and debug output in this same switches tag. For more information on this topic, see Recipe 8.1.

See Also

See Recipes 8.1 and 8.3; see the "BooleanSwitch Class" and "trace and debug Settings Schema" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net