Export (0) Print
Expand All

BooleanSwitch Class

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

For a list of all members of this type, see BooleanSwitch Members.

System.Object
   System.Diagnostics.Switch
      System.Diagnostics.BooleanSwitch

[Visual Basic]
Public Class BooleanSwitch
   Inherits Switch
[C#]
public class BooleanSwitch : Switch
[C++]
public __gc class BooleanSwitch : public Switch
[JScript]
public class BooleanSwitch extends Switch

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.

Remarks

When the BooleanSwitch constructor cannot find initial switch settings, the new switch is disabled (false) by default. The Enabled property can be used to get the current value of a switch.

You must enable tracing or debugging in order to use switches with the conditionally compiled methods of the Trace or Debug classes. 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 using 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.
Note   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.

You can set the level of your BooleanSwitch either directly in code using the Enabled property or by editing the configuration file corresponding to the name of your application. When you change the value of the switch using the configuration file, you do not need to recompile your source. Using the configuration 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 (Shared in Visual Basic) 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.

[Visual Basic] 
' 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

[C#] 
// 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");
}


[C++] 
public __gc class BooleanSwitchTest
{
   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch* dataSwitch = new BooleanSwitch(S"Data", S"DataAccess module");

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

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

[JScript] 
// Class level declaration.
 /* Create a BooleanSwitch for data.*/
 static var dataSwitch : BooleanSwitch = new BooleanSwitch("Data", "DataAccess module");
 
 static public function MyMethod(location : String) {
    // Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static function Main() {
    // Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main");
 }

Requirements

Namespace: System.Diagnostics

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

BooleanSwitch Members | System.Diagnostics Namespace | Switch | TraceSwitch | Debug | Trace

Show:
© 2014 Microsoft