Enabling or Disabling Switches

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Appendix B.  Tracing and Debugging in .NET


You can use settings in your application configuration file to enable or disable a switch at startup. This can also be done programmatically.

Configuration File Switch Settings

You can set the switch's initial setting in the application's configuration file.

 <?xml version="1.0"?> <configuration>     <system.diagnostics>         <trace indentsize="15" />             <switches>  <add name="DebugSwitch" value = "1" />   <add name="TraceSwitch" value = "2" />  </switches>     </system.diagnostics> </configuration> 

If no values are found, the initial value of the DebugSwitch's Enabled property is set to false and the TraceSwitch's Level property is set to off.

Programmatic Switch Settings

The Enabled property of the DebugSwitch can be set to true or false. The Level property of the TraceSwitch can be set to one of the options of the TraceLevelEumeration: TraceOff , TraceError , TraceWarning , TraceInfo , TraceVerbose . You can get the level of the TraceSwitch's setting by examining the TraceError , TraceWarning , TraceInfo , TraceVerbose properties.

Using Switches to Control Output

You can test the value of the switch before you write, debug, or trace output. You can do this with an if statement, or as an argument to one of the Trace or Debug classes' methods .

 Trace.WriteLineIf(TraceLevelSwitch.TraceError, _    "TraceError!") Trace.WriteLineIf(TraceLevelSwitch.TraceWarning, _    "TraceWarning!") Trace.WriteLineIf(TraceLevelSwitch.TraceInfo, _    "InfoMessage!") Trace.WriteLineIf(TraceLevelSwitch.TraceVerbose, _    "VerboseMessage!") 

Since you can set these values outside of your program's code, you can select the circumstances under which you get a particular level of debug or trace output. For example, you can turn on TraceVerbose output if you really need a high level of diagnostics, but turn it off after you have found the problem.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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