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

What is the difference between a "switch" and a "filter" in a trace in .NET? They seem to work in a 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>
+5
source share
1 answer

It overlaps a bit. A <filter>stands for the specific class you are writing derived from TraceFilter. What you can use to suppress trace output is anything possible. It always applies to a specific TraceListener.

<switches> TraceSwitch. , . , <switches> "", . , TraceSource. . "", , .

+6

All Articles