Provides an abstract base class to create new debugging and tracing switches.
Assembly: System (in System.dll)
Thetype exposes the following members.
|Attributes||Gets the custom switch attributes defined in the application configuration file.|
|Description||Gets a description of the switch.|
|DisplayName||Gets a name used to identify the switch.|
|SwitchSetting||Gets or sets the current setting for this switch.|
|Value||Gets or sets the value of the switch.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetSupportedAttributes||Gets the custom attributes supported by the switch.|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|OnSwitchSettingChanged||Invoked when the SwitchSetting property is changed.|
|OnValueChanged||Invoked when the Value property is changed.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
A switch provides an efficient mechanism for controlling tracing and debugging output at run time using external settings. The class implements default behavior for switches, allowing you to change the switch level at run time.
This class is the base class for the BooleanSwitch, SourceSwitch and the TraceSwitch classes. These switches meet most debugging and tracing needs. For more information about trace switches, see Trace Switches.
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 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.
To set the level of your switch, edit the configuration file that corresponds to the name of your application. Within this file, you can add a switch and set its value, remove a switch, 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="true" /> </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.Notes to Inheritors
If you need trace levels, or mechanisms for setting switch levels different from those provided by BooleanSwitch, SourceSwitch and TraceSwitch, you can inherit from . When inheriting from this class, you must implement the SwitchSetting method.
The following example shows how to define a new class with four levels of tracing that can be used to trace a call stack. You can use the switch to instrument your application to log each time the method is entered or exited.
The first example creates the enumeration used to set the level of the switch.
The following example creates the new switch. The code implements a Level property to set the value of the new switch. Level calls the protected property SwitchSetting that assigns the value to the new switch. This example also implements two assessor properties to get the assigned value of the switch.
The following example creates a new switch in Main. It creates a new switch and assigns it a value. Then, depending on the switch settings, it outputs debugging messages for entering and leaving the method.