Creating and Initializing 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 BooleanSwitch class and the TraceSwitch class. You would use BooleanSwitch if you care only about whether or not a tracing message appears; you would use TraceSwitch if you needed to discriminate between levels of tracing. If a TraceSwitch is used you can define your own debugging messages and associate them with different trace levels. You can use both types of switch 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, it is recommended 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 allows 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
- Define a switch as either type BooleanSwitch or type TraceSwitch and set the name and description of the switch.
- Configure your trace switch. For more information, see Configuring Trace Switches.
The following code creates two switches, one of each type:
' Visual Basic Dim dataSwitch As New BooleanSwitch("Data", "DataAccess module") Dim generalSwitch As New TraceSwitch("General", "Entire application") // C# System.Diagnostics.BooleanSwitch dataSwitch = new System.Diagnostics.BooleanSwitch("Data", "DataAccess module"); System.Diagnostics.TraceSwitch generalSwitch = new System.Diagnostics.TraceSwitch("General", "Entire application");