System.Diagnostics.BooleanSwitch Class

Assembly: System.dll
Namespace: System.Diagnostics
Summary
Provides a simple on/off switch that controls debugging and tracing output.
C# Syntax:
public class BooleanSwitch : Switch
Remarks
When the BooleanSwitch constructor cannot find initial switch settings, the new switch is disabled (false) by default. The BooleanSwitch.Enabled property can be used to get the current value of a switch.

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.

To set the level of your BooleanSwitch, edit the configuration file corresponding to the name of your application. Within this file, you can add a switch and set its value, remove a switch, or clear all switches previously set by the application. The configuration file should be formatted like the following example:

          <configuration>
           <system.diagnostics>
           <switches>
           <add name="mySwitch" value="10" />
           <add name="myNewSwitch" value="20" />
           <remove name="mySwitch" />
           <clear/>
           </switches>
           </system.diagnostics>
           </configuration>
        


Note To improve performance, you can make BooleanSwitch members static in your class.
Example
The following example creates a BooleanSwitch and uses the switch to determine whether to print an error message. You create the switch at the class level. The Main method passes its location to MyMethod , which prints an error message and where the error occurred.
 // Class level declaration.
 /* Create a BooleanSwitch for data. This switch is enabled by using values 
  * stored in the Registry or in environmental variables. */
 static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");
 
 static public void MyMethod(string location) {
    //Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static void Main(string[] args) {
    //Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main");
 }


    
See also:
System.Diagnostics Namespace | Switch | TraceSwitch | Debug | Trace

System.Diagnostics.BooleanSwitch Member List:

Public Constructors
ctor #1 Initializes a new instance of the BooleanSwitch class.
Public Properties
Description
(inherited from System.Diagnostics.Switch)
Read-only

See base class member description: System.Diagnostics.Switch.Description


Gets a description of the switch.
DisplayName
(inherited from System.Diagnostics.Switch)
Read-only

See base class member description: System.Diagnostics.Switch.DisplayName


Gets a name used to identify the switch.
Enabled Read-write

Specifies whether the switch is enabled or disabled.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Properties
SwitchSetting
(inherited from System.Diagnostics.Switch)
Read-write

See base class member description: System.Diagnostics.Switch.SwitchSetting


Gets or sets the current setting for this switch.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.
OnSwitchSettingChanged
(inherited from System.Diagnostics.Switch)
See base class member description: System.Diagnostics.Switch.OnSwitchSettingChanged


Raises the SwitchSettingChanged event.

Hierarchy:


System.Diagnostics.BooleanSwitch Member Details

ctor #1
Summary
Initializes a new instance of the BooleanSwitch class.
C# Syntax:
public BooleanSwitch(
   string displayName,
   string description
);
Parameters:

displayName

The name to display on a user interface.

description

The description of the switch.

Remarks
When you create a BooleanSwitch, displayName is used to find initial switch settings. If the constructor cannot find initial settings, the BooleanSwitch.Enabled property is set to disabled (false).

To set the level of your BooleanSwitch, edit the configuration file corresponding to the name of your application. Within this file, you can add a switch and set its value, remove a switch, or clear all switches previously set by the application. The configuration file should be formatted like the following example:

              <configuration>
                  <system.diagnostics>
                     <switches>
                        <add name="mySwitch" value="10" />
                        <add name="myNewSwitch" value="20" />
                        <remove name="mySwitch" />
                        <clear/>
                     </switches>
                  </system.diagnostics>
               </configuration>
            


Note Your instantiated switches should be static.
Example
The following example creates a BooleanSwitch and uses the switch to determine whether to print an error message. The switch is created at the class level. The Main method passes its location to MyMethod , which prints an error message and where the error occurred.
 // Class level declaration.
 /* Create a BooleanSwitch for data. This switch is enabled by using values 
  * stored in the Registry or in environmental variables. */
 static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");
 
 static public void MyMethod(string location) {
    //Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static void Main(string[] args) {
    //Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main");
 }


    
See also:
BooleanSwitch | Switch | Debug | Trace

Return to top


Property: Description (read-only)
Inherited
See base class member description: System.Diagnostics.Switch.Description

Summary
Gets a description of the switch.
C# Syntax:
public string Description {get;}
Remarks
This property should indicate the function of the switch. For example, "Enables tracing for a directory watcher component."
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Property: DisplayName (read-only)
Inherited
See base class member description: System.Diagnostics.Switch.DisplayName

Summary
Gets a name used to identify the switch.
C# Syntax:
public string DisplayName {get;}
Remarks
When you create a new Switch object, the Switch.DisplayName finds initial switch settings. For more information, see the Switch.#ctor constructor and the TraceSwitch Configuration topic in the Visual Studio documentation.
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Property: Enabled (read-write)
Summary
Specifies whether the switch is enabled or disabled.
C# Syntax:
public bool Enabled {get; set;}
Remarks
By default, this field is set to disabled (false). To enable the switch, assign this field the value of true. To disable the switch, assign it false.
Example
The following example creates a BooleanSwitch and uses the switch to determine whether to print an error message. The switch is created at the class level. The Main method passes its location to MyMethod , which prints an error message and the location where the error occurred.
//Class level declaration.
 /* Create a BooleanSwitch for data. This switch is enabled by using values 
  * stored in the Registry or in environmental variables. */
 static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");
 
 static public void MyMethod(string location) {
    //Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static void Main(string[] args) {
    //Run the method that writes an error message specifying the location of the error.
    MyMethod("in Main");
 }
 

    
See also:
BooleanSwitch | Switch | Debug | Trace

Return to top


Property: SwitchSetting (read-write)
Inherited
See base class member description: System.Diagnostics.Switch.SwitchSetting

Summary
Gets or sets the current setting for this switch.
C# Syntax:
protected int SwitchSetting {get; set;}
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~BooleanSwitch();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: OnSwitchSettingChanged()
Inherited
See base class member description: System.Diagnostics.Switch.OnSwitchSettingChanged

Summary
Raises the SwitchSettingChanged event.
C# Syntax:
protected virtual void OnSwitchSettingChanged();
Remarks
The SwitchSettingChanged event is raised any time the Switch.SwitchSetting property is changed. It is invoked the first time a switch reads its value from the configuration file and is invoked again each time the switch's value is changed.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.