Tracing event logs in visual programming c#.pptx

sanasaeed84 283 views 10 slides Sep 03, 2024
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Tracing event logs with Boolean switches and Trace switch classes in C#


Slide Content

Tracing event logs using Boolean switches and trace switch classes

Trace switch A trace switch is a way to control tracing and debugging output at runtime using various settings . The Switch class is the parent of the BooleanSwitch and TraceSwitch classes . Despite our being able to derive and create custom switches, the BooleanSwitch and TraceSwitch classes will be satisfactory for our debugging and tracing needs.

The TraceSwitch class provides per assembly, per module, or per class tracing. This is more powerful and easier than the single-trace options because it lets you specify various output options at once. Therefore , the extreme debugging or tracing outputs do not overwhelm us with the various options that we have specified when we don't want them.  Table below describes members of the TraceSwitch class. The TraceSwitch constructor takes two parameters: the descriptive display name ("descriptive trace switch name") and the switch name. 

We generally define the switches for the executable in the EXE.CONFIG configuration file. The application configuration file name is <program >. Inside switch elements of the XML file, the "name" attribute refers to the switch name and the "value" attribute is the numerical value the corresponding trace level is set to. we can also define the autoflush and the indent size parameters in the configuration file. 

<?xml version="1.0" encoding="UTF-8" ?> <configuration>   < system.diagnostics >     <switches>       <add name="Switch1" value="3" />       <add name="Switch2" value="2" />     </switches>     <!—additional diagnostics -->     <trace  autoflush ="true"  indentsize ="3" />   </ system.diagnostics > </configuration>

Each assembly should have its own trace switch.  Possible TraceSwitch tracing levels are as follows: 0-None 1-Errors only; TraceError 2-Warning (warnings and errors); TraceWarning 3-Info (warnings, errors, and information); TraceInfo 4-Verbose (all kinds of traces); TraceVerbose

   TraceSwitch   MySwitch = new  MySwitch ("Switch1", "my switch1 at Info mode");             // do you see which one will run?              Trace.WriteLineIf ( MySwitch.TraceError ,             "Error tracing is on!");              Trace.WriteLineIf ( MySwitch.TraceWarning ,             "Warning tracing is on!");              Trace.WriteLineIf ( MySwitch.TraceInfo ,             "Info tracing is on!");              Trace.WriteLineIf ( MySwitch.TraceVerbose , " VerboseSwitching is on!");

Boolean switches can be used as a switch in your code to either turn on or turn off the printing of a debug statement. Possible BooleanSwitch tracing levels are as follows: 0-False; Disabled 1-True; Enabled

Listing 21.13: Using BoolSwitch                BooleanSwitch   boolSwitch = new  BooleanSwitch ("Switch3", "my boolean switch");              Debug.WriteLine ("DEBUG");              Trace.WriteLine ("TRACE");              Trace.WriteLineIf ( boolSwitch.Enabled , " boolSwitch ");