Explain how to configure Trace switches in the application's .config file? Give the code snippet of config file.

 Posted by Tripati_tutu on 10/12/2010 | Category: ASP.NET Interview questions | Views: 4470 | Points: 40

Switches are configured using .config file. Trace switches can be configured and the trace output can be enabled or disabled in an application with the help of .config file. Configuring involves changing the value of the switch from an external source after being initialized. The values of the switch objects can be changed using the .config file.

TraceSwitch offers five levels of tracing from 0 to 4 i.e. it expose the following properties.

• TraceOff
• TraceError
• TraceWarning
• TraceInfo
• TraceVerbose

This class provides support for multiple levels instead of the simple on/off control offered by the BooleanSwitch class.

• Off - 0 Outputs no messages to Trace Listeners.
• Error - 1 Outputs only error messages to Trace Listeners.
• Warning - 2 Outputs error and warning messages to Trace Listeners.
• Info - 3 Outputs informational, warning and error messages to Trace Listeners.
• Verbose - 4 Outputs all messages to Trace Listeners.

Code Snippet of config file:

<add name=”LevelSwitch” value=”3” />

To construct a TraceSwitch:
TraceSwitch tSwitch=new TraceSwitch (“LevelSwitch”, “Trace Levels”);

System.Diagnostics.Trace.WriteIf (tSwitch.TraceInfo, “The Switch is 3 or more!”);

Here the TraceSwitch class returns true if the switch is at the same level or at the higher level than the property’s value. Here the TraceInfo property will return true if the switch value is set to 3 or more than 3.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response