After your application has been distributed, you can still enable or disable trace output by configuring the trace switches in your application. Configuring a switch means changing its value from an external source after it has been initialized. You can change the values of the switch objects using the configuration file. You configure a trace switch to turn it on and off, or to set its level, determining the amount and type of messages it passes along to listeners.
Your switches are configured using the .config file. For a Web application, this is the Web.config file associated with the project. In a Windows application, this file is named (application name).exe.config. In a deployed application, this file must reside in the same folder as the executable.
When your application executes the code that creates an instance of a switch for the first time, it checks the configuration file for trace-level information about the named switch. The tracing system examines the configuration file only once for any particular switch — the first time your application creates the switch.
In a deployed application, you enable trace code by reconfiguring switch objects when your application is not running. Typically this involves turning the switch objects on and off or by changing the tracing levels, and then restarting your application.
When you create an instance of a switch, you also initialize it by specifying two arguments: a displayName argument and a description argument. The displayName argument of the constructor sets the Switch..::.DisplayName property of the Switch class instance. The displayName is the name that is used to configure the switch in the .config file, and the description argument should return a brief description of the switch and what messages it controls.
In addition to specifying the name of a switch to configure, you must also specify a value for the switch. This value is an Integer. For BooleanSwitch, a value of 0 corresponds to Off, and any nonzero value corresponds to On. For TraceSwitch, 0,1,2,3, and 4 correspond Off, Error, Warning, Info, and Verbose, respectively. Any number greater than 4 is treated as Verbose, and any number less than zero is treated as Off.
Note: |
|---|
In the .NET Framework version 2.0, you can use text to specify the value for a switch. For example, true for a BooleanSwitch or the text representing an enumeration value such as Error for a TraceSwitch. The line <add name="myTraceSwitch" value="Error" /> is equivalent to <add name="myTraceSwitch" value="1" />. |
In order for end users to be able to configure an application's trace switches, you must provide detailed documentation on the switches in your application. You should detail which switches control what and how to turn them on and off. You should also provide your end user with a .config file that has appropriate Help in the comments.