See Also: TraceSwitch Members
You can use a trace switch to filter out messages based on their importance. The System.Diagnostics.TraceSwitch class provides the TraceSwitch.TraceError, TraceSwitch.TraceWarning, TraceSwitch.TraceInfo, and TraceSwitch.TraceVerbose properties to test the level of the switch. The TraceSwitch.Level property gets or sets the switch's System.Diagnostics.TraceLevel.
You can set the level of a System.Diagnostics.TraceSwitch through the application configuration file and then use the configured System.Diagnostics.TraceSwitch level in your application. Alternately, you can create a System.Diagnostics.TraceSwitch in your code and set the level directly to instrument a specific section of code.
To configure a System.Diagnostics.TraceSwitch, edit the configuration file for your application. In 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 configuration section defines a System.Diagnostics.TraceSwitch with the Switch.DisplayName set to mySwitch, and the TraceSwitch.Level set to 1, which corresponds to the enumeration value TraceLevel.Error.
In the .NET Framework version 2.0, you can use text to specify the value for a switch. For example, true for a System.Diagnostics.BooleanSwitch or the text representing an enumeration value, such as Error for a System.Diagnostics.TraceSwitch. The line <add name="mySwitch" value="Error" /> is equivalent to <add name="mySwitch" value="1" />.
In your application, you can use the configured switch level by creating a System.Diagnostics.TraceSwitch with the same name, as shown in the following example:
code reference: Classic TraceSwitch.TraceError Example#3
By default, the switch TraceSwitch.Level property is set using the value specified in the configuration file. If the erload:System.Diagnostics.TraceSwitch.#ctor constructor cannot find initial switch settings in the configuration file, the TraceSwitch.Level of the new switch defaults to TraceLevel.Off.
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 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.
These debug and trace compiler switches are not required when using the System.Diagnostics.TraceSwitch class in isolation. They are only required in conjunction with System.Diagnostics.Trace or System.Diagnostics.Debug methods that are conditionally compiled.
To improve performance, you can make System.Diagnostics.TraceSwitch members static in your class.