System.Diagnostics.TraceSwitch Class provides a multilevel switch to control tracing and debug output without recompiling your code. The tracing dumps can be quite verbose. For applications that are constantly running you run the risk of overloading the machine and the hard drive. TraceSwitch does not directly control the Trace statements, you have to pass one of its properties into a WriteLineIf or WriteIf method.
The TraceSwitch constructor take three values:
- Display Name
- Default Trace Value
The default value is 0
You can set the level of a TraceSwitch through the application configuration file and then use the configured TraceSwitch level in your application. Alternately, you can create a TraceSwitch in your code and set the level directly to instrument a specific section of code. Five levels range from None to Verbose, allowing you to fine-tune the tracing activities.
- TraceLevel.Error: Output error-handling messages.
- TraceLevel.Info Output: informational messages, warnings, and error-handling messages.
- TraceLevel.Off: Output no tracing and debugging messages.
- TraceLevel.Verbose: Output all debugging and tracing messages.
- TraceLevel.Warning: Output warnings and error-handling messages.
To configure a TraceSwitch , edit the application 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: