Using TraceSwitch

You can use the TraceSwitch class to control the detail of messages produced by your Trace statements. TraceSwitch does not directly control the Trace statements, you have to pass one of its properties into a WriteLineIf or WriteIf method.

The TraceSwitch constructor take three values:

Display Name
Description
Default Trace Value
The default value is 0

The TraceSwitch can be set at a number of levels:

TraceLevel.Off
TraceLevel.Error
TraceLevel.Warning
TraceLevel.Info
TraceLevel.Verbose

One of the main benefits is that you can control the TraceSwitch from the application or web config file:



    
        
            
            
        
    


Once you have create your TraceSwitch:


            string displayName = "BusinessLogic.TraceLevelSwitch";
            string description = "Business Logic Trace Switch";
            string defaultSwitchValue = "0"; // TraceLevel.Off

            TraceSwitch traceSwitch = new TraceSwitch(displayName, description, defaultSwitchValue);

You can then use one of it properties:

traceSwitch.Level
This returns a value of enum type TraceLevel
traceSwitch.TraceError
traceSwitch.TraceWarning
traceSwitch.TraceInfo
traceSwitch.TraceVerbose
Are all boolean

With your tracing statements:


Trace.WriteLineIf(traceSwitch.TraceVerbose, "Switch is set to show Verbose messages");
//or
Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Off), "Switch is set to Off.");

Here is the full code example:


            string displayName = "BusinessLogic.TraceLevelSwitch";
            string description = "Business Logic Trace Switch";
            string defaultSwitchValue = "0"; // Off

            TraceSwitch traceSwitch =
                new TraceSwitch(displayName, description, defaultSwitchValue);

            Trace.Listeners.Add(new TextWriterTraceListener("Trace.log"));
            Trace.AutoFlush = true;

            Trace.WriteLine(DateTime.Now.ToString());
            Trace.WriteLine("");
            Trace.WriteLine("TraceSwitch.DisplayName: " + traceSwitch.DisplayName);
            Trace.WriteLine("TraceSwitch.Description: " + traceSwitch.Description);
            Trace.WriteLine("TraceSwitch.Level: " + traceSwitch.Level);
            Trace.WriteLine("");

            Trace.WriteLineIf(traceSwitch.TraceError, "Switch is set to show Error messages");

            Trace.WriteLineIf(traceSwitch.TraceWarning, "Switch is set to show Warning messages");

            Trace.WriteLineIf(traceSwitch.TraceInfo, "Switch is set to show Info messages");

            Trace.WriteLineIf(traceSwitch.TraceVerbose, "Switch is set to show Verbose messages");

            Trace.WriteLine("");

            Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Off), "Switch is set to Off.");

            Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Error), "Switch is set to Error.");

            Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Warning), "Switch is set to Error and Warning.");

            Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Info), 
                                      "Switch is set to Error, Warning and Info.");

            Trace.WriteLineIf((traceSwitch.Level == TraceLevel.Verbose), 
                                      "Switch is set to Error, Warning, Info and Verbose.");

            Trace.WriteLine("");

This is the output with the TraceSwitch set to TraceLevel.Warning or 2:


23/09/2010 09:24:43

TraceSwitch.DisplayName: BusinessLogic.TraceLevelSwitch
TraceSwitch.Description: Business Logic Trace Switch
TraceSwitch.Level: Warning

Switch is set to show Error messages
Switch is set to show Warning messages

Switch is set to Error and Warning.

To use TraceSwitch you require the System.Diagnostics namespace.



Comments

  1. fabrizio March 9th

    Comment Arrow

    Hi, is there a way to output trace data to a file?


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.