Monday, May 4, 2009

Trace view /Debug View in .NET

Microsoft introduced two switches in .NET 2.0 and higher.
These two switches are BooleanSwitch and TraceSwitch
Usage in the code is as follows:
private static BooleanSwitch bw = new BooleanSwitch("EnableTrace", "Boolean Switch for logging");
private static TraceSwitch tw = new TraceSwitch("TraceSwitchLevel", "Trace Switch for Level of Logs");

Also define these switches in the .config file for the application as follows:

"-system.diagnostics-
-switches-
-!-- This switch controls data messages. In order to receive data trace messages, change value="0" to value="1" --
-add name="EnableTrace" value="0">
-!-- This switch controls general messages. In order to receive general trace messages change the value to the appropriate level. Disable Trace = "0" Error = "1" Errors and Warnings = "2" More detailed error information = "3" Verbose trace information = "4" --
-add name="TraceSwitchLevel" value="4"-
-/add>
-/add>"

Note that the name of the switches used in the config files and in the code are same. This is important. Do not use a different names in config and code files.
TraceSwitch are incremental. If you specify a values of 3 then it will include 2 and 1 also and so on.

You can write log in a txt file or if you do not specify any file then all the logs can be seen in the DebugView utility provided by microsoft. You can download it from http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

-Abinaw

No comments:

Post a Comment