Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

How to: Create and Initialize Trace Switches

In order to use trace switches, you must first create them and place them in your code. There are two predefined classes from which you can create switch objects: the System.Diagnostics.BooleanSwitch class and the System.Diagnostics.TraceSwitch class. You would use BooleanSwitch if you care only about whether or not a tracing message appears; you would use TraceSwitch if you want to discriminate between levels of tracing. If you use a TraceSwitch, you can define your own debugging messages and associate them with different trace levels. You can use both types of switches with either tracing or debugging. By default, a BooleanSwitch is disabled and a TraceSwitch is set to level TraceLevel.Off. Trace switches can be created and placed in any part of your code that might use them.

Although you can set trace levels and other configuration options in code, we recommend that you use the configuration file to manage the state of your switches. This is because managing the configuration of your switches in the configuration system gives you greater flexibility — you can turn on and off various switches and change levels without recompiling your application. For more information, see Configuring Trace Switches.

To create and initialize a trace switch

  1. Define a switch as either type System.Diagnostics.BooleanSwitch or type System.Diagnostics.TraceSwitch and set the name and description of the switch.

  2. Configure your trace switch. For more information, see Configuring Trace Switches.

    The following code creates two switches, one of each type:

    System.Diagnostics.BooleanSwitch dataSwitch = 
       new System.Diagnostics.BooleanSwitch("Data", "DataAccess module");
    System.Diagnostics.TraceSwitch generalSwitch = 
       new System.Diagnostics.TraceSwitch("General", 
       "Entire application");
    
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.