How can I get the current software switch programmatically?

In my web.config , I have:

 <system.diagnostics> <switches> <add name="logLevelSwitch" value="1" /> </switches> </system.diagnostics> 

Is there a way I could call, for example:

System.Diagnostics.TraceSwitch["logLevelSwitch"] to get the current value?

+7
source share
1 answer

Once you have determined the switch value in your web.config , it is easy to get this value from your application by creating TraceSwitch with the same name:

 private static TraceSwitch logSwitch = new TraceSwitch("logLevelSwitch", "This is your logLevelSwitch in the config file"); public static void Main(string[] args) { // you can get its properties value then: Console.WriteLine("Trace switch {0} is configured as {1}", logSwitch.DisplayName, logSwitch.Level.ToString()); // and you can use it like this: if (logSwitch.TraceError) Trace.WriteLine("This is an error"); // or like this also: Trace.WriteLineIf(logSwitch.TraceWarning, "This is a warning"); } 

Also, for this to work, as per the documentation:

You must enable tracing or debugging in order to use the switch. The following syntax is compiler specific. If you use compilers other than C # or Visual Basic, see the documentation for your compiler.

To enable 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 command line compiler.

To enable tracing using in C #, add the /d:TRACE flag to the compiler command line when compiling the code, or add #define TRACE to the top of the file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

+4
source

All Articles