System.Diagnostics.BooleanSwitch Class

Provides a simple on/off switch that controls debugging and tracing output.

See Also: BooleanSwitch Members


public class BooleanSwitch : Switch


You can use a Boolean trace switch to enable or disable messages based on their importance. Use the BooleanSwitch.Enabled property to get the current value of the switch.

You can enable or disable a System.Diagnostics.BooleanSwitch through the application configuration file and then use the configured System.Diagnostics.BooleanSwitch value in your application. Alternately, you can create a System.Diagnostics.BooleanSwitch in your code and set the BooleanSwitch.Enabled property directly to instrument a specific section of code.

To configure a System.Diagnostics.BooleanSwitch, edit the configuration file that corresponds to the name of your application. Within this file, you can add or remove a switch, set a switch's value, or clear all the switches previously set by the application. The configuration file should be formatted like the following example.


      <add name="mySwitch" value="1"/>

This example configuration section defines a System.Diagnostics.BooleanSwitch with the Switch.DisplayName property set to mySwitch and the BooleanSwitch.Enabled value set to true. Within your application, you can use the configured switch value by creating a System.Diagnostics.BooleanSwitch with the same name, as shown in the following code example.

code reference: Classic BooleanSwitch.BooleanSwitch Example#2

By default, the BooleanSwitch.Enabled property is set using the value specified in the configuration file. Configure the switch with a value of 0 to set the BooleanSwitch.Enabled property to false; configure the switch with a nonzero value to set the BooleanSwitch.Enabled property to true. If the System.Diagnostics.BooleanSwitch constructor cannot find initial switch settings in the configuration file, the BooleanSwitch.Enabled property of the new switch is set to false by default.

You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.


These debug and trace compiler switches are not required when using the System.Diagnostics.BooleanSwitch class in isolation. They are only required in conjunction with System.Diagnostics.Trace or System.Diagnostics.Debug methods that are conditionally compiled.

For more information on instrumenting your application, see System.Diagnostics.Debug and System.Diagnostics.Trace. For more information about configuring and using trace switches, see Trace Switches.


To improve performance, you can make System.Diagnostics.BooleanSwitch members static in your class.


Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,,