Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Instrumenting Applications with .NET Tracing : Page 3

If you really want to know how a production application is running, try turning on .NET's tracing capabilities. You can use .NET's built-in tracing or build your own.


advertisement
Boolean Switches
A BooleanSwitch will allow you to produce trace output only when the switch is true. When all tracing is turned on, the output can quickly use a lot of resources if saving output to file or some other persistent store. If your application is small or you don't hold log information longer than immediate diagnosis, a BooleanSwitch will work fine. The following code shows how to use a BooleanSwitch.

using System; using System.Diagnostics; class BooleanSwitches { static BooleanSwitch boolSwitch = new BooleanSwitch( "MyBooleanSwitch", "Bool Switch Demo"); static void Main() { Trace.WriteLineIf( boolSwitch.Enabled, "Testing Boolean Switch.", boolSwitch.Description); Console.ReadLine(); } }

The preceding code is very simple—if boolSwitch.Enabled evaluates to true, then the string parameter to the WriteLineIf method is sent to output.

When instantiating the boolSwitch variable, you set the first parameter to a configuration file parameter named MyBooleanSwitch. As shown in the XML snippet below, the System.Diagnostics element contains a switches element where you can add a switch statement. In this case there is an element for MyBooleanSwitch and it is turned on. A value of 1 turns the switch on and 0 turns the switch off. You can add multiple switches to the configuration file to help manage tracing at a more granular level in different parts of your application.

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.diagnostics> <switches> <add name="MyBooleanSwitch" value="1" /> </switches> </system.diagnostics> </configuration>

Trace Switches
The problem with a BooleanSwitch approach to instrumentation is that it is all-or-nothing. Most of the time, you need more granularity in your approach. For example, perhaps during normal operations you only want to know about error conditions. However, to diagnose a problem, you need the ability to trace more information for a short period of time. The level of information you need at any given time could vary depending on what you need to know. Here you'll find value using the TraceSwitch.

The TraceSwitch has five levels of control, defined by the TraceLevel enum (with corresponding value): Off (0), Error (1), Warning (2), Info (3), and Verbose (4). You determine when to use each switch level. To help out, I'll explain how I use them. I use Error to detect errors in code, catch filters, and global exception handlers. I rarely use Warning, but I use it for strange situations that aren't really errors but I should pay attention to them. If that sounds ambiguous, it is; which is why I rarely use Warning. I use Info to enter and exit methods. I log state conditions in an algorithm with Verbose. Be aware that too many trace statements could make an algorithm harder to read, so use them only where you think they have debugging value. Basically, think about what information you need to detect problems and debug your application if something went wrong. The following TraceSwitches class shows how to use a TraceSwitch in your code.



class TraceSwitches { static TraceSwitch traceSwitch = new TraceSwitch( "MyTraceSwitch", "Trace Switch Demo"); static void Main() { Trace.WriteLineIf( traceSwitch.TraceError, "Testing Error Trace Switch.", traceSwitch.Description); Trace.WriteLineIf( traceSwitch.TraceWarning, "Testing Warning Trace Switch.", traceSwitch.Description); Trace.WriteLineIf( traceSwitch.TraceInfo, "Testing Info Trace Switch.", traceSwitch.Description); Trace.WriteLineIf( traceSwitch.TraceVerbose, "Testing Verbose Trace Switch.", traceSwitch.Description); Console.ReadLine(); } }

Use a TraceSwitch switch when you need to filter the amount of output based on an increasing level of detail and severity.

When your code declares the TraceSwitch variable, it is instantiated with the MyTraceSwitch parameter, which corresponds to the same entry in the switches element of the configuration file shown below.

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.diagnostics> <switches> <add name="MyTraceSwitch" value="4" /> </switches> </system.diagnostics> </configuration>

This configuration file sets MyTraceSwitch to 4, which turns on tracing for Verbose and every level below it. As implied, the trace level setting in the configuration file turns on tracing for the level it is set at in addition to all lower levels.

The TraceSwitch class has Boolean properties that correspond to each trace level. The preceding example code demonstrates how to use the traceSwitch variable as the first parameter to WriteLineIf to determine if a specific trace level is set before logging output.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date