.NET Framework Class Library
BooleanSwitch Class

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

Inheritance Hierarchy
SystemObject
   System.DiagnosticsSwitch
    System.DiagnosticsBooleanSwitch

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)
Syntax
<[%$TOPIC/82eya946_en-us_VS_110_2_0_0_0_0%](GetType([%$TOPIC/82eya946_en-us_VS_110_2_0_0_0_1%]))> _
Public Class BooleanSwitch _
	Inherits [%$TOPIC/82eya946_en-us_VS_110_2_0_0_0_2%]
[[%$TOPIC/82eya946_en-us_VS_110_2_0_1_0_0%](typeof([%$TOPIC/82eya946_en-us_VS_110_2_0_1_0_1%]))]
public class BooleanSwitch : [%$TOPIC/82eya946_en-us_VS_110_2_0_1_0_2%]
[[%$TOPIC/82eya946_en-us_VS_110_2_0_2_0_0%](typeof([%$TOPIC/82eya946_en-us_VS_110_2_0_2_0_1%]))]
public ref class BooleanSwitch : public [%$TOPIC/82eya946_en-us_VS_110_2_0_2_0_2%]
[<[%$TOPIC/82eya946_en-us_VS_110_2_0_3_0_0%](typeof([%$TOPIC/82eya946_en-us_VS_110_2_0_3_0_1%]))>]
type BooleanSwitch =  
    class 
        inherit [%$TOPIC/82eya946_en-us_VS_110_2_0_3_0_2%] 
    end

The BooleanSwitch type exposes the following members.

Constructors
  NameDescription
Public method BooleanSwitch(String, String)Initializes a new instance of the BooleanSwitch class with the specified display name and description.
Public method BooleanSwitch(String, String, String)Initializes a new instance of the BooleanSwitch class with the specified display name, description, and default switch value.
Top
Properties
  NameDescription
Public property AttributesGets the custom switch attributes defined in the application configuration file. (Inherited from Switch.)
Public property DescriptionGets a description of the switch. (Inherited from Switch.)
Public property DisplayNameGets a name used to identify the switch. (Inherited from Switch.)
Public property EnabledGets or sets a value indicating whether the switch is enabled or disabled.
Protected property SwitchSettingGets or sets the current setting for this switch. (Inherited from Switch.)
Protected property ValueGets or sets the value of the switch. (Inherited from Switch.)
Top
Methods
  NameDescription
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Protected method GetSupportedAttributesGets the custom attributes supported by the switch. (Inherited from Switch.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnSwitchSettingChangedInvoked when the SwitchSetting property is changed. (Inherited from Switch.)
Protected method OnValueChangedDetermines whether the new value of the Value property can be parsed as a Boolean value. (Overrides SwitchOnValueChanged.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

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

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

To configure a 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.

<configuration>
  <system.diagnostics>
    <switches>
      <add name="mySwitch" value="1"/>
    </switches>
  </system.diagnostics>
</configuration>

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

Private Shared boolSwitch As new BooleanSwitch("mySwitch", _
    "Switch in config file")

Public Shared Sub Main( )
    '...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString())
    If boolSwitch.Enabled = True Then 
        '... 
    End If 
End Sub
private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
    "Switch in config file");

public static void Main( )
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}
private:
    static BooleanSwitch^ boolSwitch = gcnew BooleanSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main( )
    {
        //...
        Console::WriteLine("Boolean switch {0} configured as {1}",
            boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
        if (boolSwitch->Enabled)
        {
            //...
        }
    }

By default, the Enabled property is set using the value specified in the configuration file. Configure the switch with a value of 0 to set the Enabled property to false; configure the switch with a nonzero value to set the Enabled property to true. If the BooleanSwitch constructor cannot find initial switch settings in the configuration file, the 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.

  • To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

  • To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

NoteNote

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

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

NoteNote

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

Examples

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.  
Private Shared dataSwitch As New BooleanSwitch("Data", "DataAccess module")


Public Shared Sub MyMethod(location As String)
    ' Insert code here to handle processing. 
    If dataSwitch.Enabled Then
        Console.WriteLine(("Error happened at " + location))
    End If 
End Sub 'MyMethod

' Entry point which delegates to C-style main function. 
Public Overloads Shared Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub 

Overloads Public Shared Sub Main(args() As String)
    ' Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main")
End Sub 'Main
// Class level declaration. 
 /* Create a BooleanSwitch for data.*/ 
 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");
 }
public ref class BooleanSwitchTest
{
private:

   /* Create a BooleanSwitch for data.*/ 
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess module" );

public:
   static void MyMethod( String^ location )
   {

      //Insert code here to handle processing. 
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }

};

int main()
{

   //Run the method which writes an error message specifying the location of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.