Provides a simple on/off switch that controls debugging and tracing output.
Assembly: System (in System.dll)
Thetype exposes the following members.
|Attributes||Gets the custom switch attributes defined in the application configuration file. (Inherited from Switch.)|
|Description||Gets a description of the switch. (Inherited from Switch.)|
|DisplayName||Gets a name used to identify the switch. (Inherited from Switch.)|
|Enabled||Gets or sets a value indicating whether the switch is enabled or disabled.|
|SwitchSetting||Gets or sets the current setting for this switch. (Inherited from Switch.)|
|Value||Gets or sets the value of the switch. (Inherited from 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 a hash function for a particular type. (Inherited from Object.)|
|GetSupportedAttributes||Gets the custom attributes supported by the switch. (Inherited from 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. (Inherited from Switch.)|
|OnValueChanged||Determines whether the new value of the Value property can be parsed as a Boolean value. (Overrides Switch.OnValueChanged().)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
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 through the application configuration file and then use the configured value in your application. Alternately, you can create a in your code and set the Enabled property directly to instrument a specific section of code.
To configure a , 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 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 with the same name, as shown in the following code example.
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 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.
To improve performance, you can make members static in your class.
The following example creates a 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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.