Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "switch" and "filter" in Tracing in .NET?

What is the difference between "switch" and "filter" in Tracing in .NET ? They seem to work in similar way.

<system.diagnostics>
    <trace autoflush="true" indentsize="5">
      <listeners>
        <add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output1.txt">
        </add>
        <remove name="Default" />
      </listeners>
    </trace>    
    <sources>
      <source name="DemoApp" switchName="DemoApp">
        <listeners>
          <add name="DemoListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\output2.txt">
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error"/>
          </add>
          <remove name="Default" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DemoApp" value="Error"/>
    </switches>
  </system.diagnostics>
like image 554
Brij Avatar asked Apr 01 '13 11:04

Brij


People also ask

What are Trace switches?

Trace switches allow you to enable, disable, and filter tracing output. They are objects that exist in your code and can be configured externally through the . config file.

What is TraceSource in c#?

TraceSource provides tracing methods that allow you to easily trace events, trace data, and issue informational traces. In . NET Framework apps, trace output from TraceSource can be controlled by configuration file settings.


1 Answers

There is a bit of overlap. A <filter> names a specific class that you write that's derived from TraceFilter. Which you can use to suppress trace output, anything is possible. It always applies to a specific TraceListener.

The <switches> element is useful to configure tracing and set the value of a TraceSwitch object. Which you then test in your code to selectively bypass trace output. Note how <switches> is "global", it doesn't apply to a specific listener. So a logical place to test the switch is in the TraceSource. A good use for a switch is to configure the tracing verbosity. Like your "Error" value would indicate that only errors are ever traced.

like image 182
Hans Passant Avatar answered Sep 22 '22 08:09

Hans Passant