Dela via


<filter> Element for <add> for <listeners> for <source>

Adds a filter to a listener in the Listeners collection for a trace source.

<configuration> Element
  <system.diagnostics> Element
    <sources> Element
      <source> Element
        <listeners> Element for <trace>
          <add> Element for <listeners> for <trace>
            <filter> Element for <add> for <listeners> for <source>

<filter 
  type="traceFilterClassName" 
  initializeData="data" />

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute

Description

type

Required attribute.

Specifies the type of the filter, which should inherit from the TraceFilter class. You can use the namespace-qualified name of the type, which corresponds to the type's FullName property, or you can use the fully qualified type name including the assembly information, which corresponds to the AssemblyQualifiedName property. For information about fully qualified type names, see Specifying Fully Qualified Type Names.

initializeData

Optional attribute.

The string passed to the constructor for the specified filter class.

Child Elements

None.

Parent Elements

Element

Description

configuration

The root element in every configuration file used by the common language runtime and .NET Framework applications.

system.diagnostics

Specifies trace listeners that collect, store, and route messages and the level where a trace switch is set.

sources

Contains trace sources that initiate tracing messages.

source

Specifies a trace source that initiates tracing messages.

listeners

Contains listeners that collect, store, and route messages. Listeners direct the tracing output to an appropriate target.

add

Adds a listener to the Listeners collection for a trace source.

Remarks

The <filter> element must be contained in an <add> element for a trace source listener that specifies the type of the listener, not just the name of a listener defined in a <sharedListeners> Element. If the listener is defined in a <sharedListeners> Element, the filter for that listener must be defined in that element.

This element can be used in the machine configuration file (Machine.config) and the application configuration file.

Example

The following example shows how to use the <filter> element to add a filter to the listener console in the Listeners collection for the trace source myTraceSource, specifying the filter event level as Error.

<configuration>
  <system.diagnostics>
    <sources>
      <source name="myTraceSource" switchName="SourceSwitch" 
        switchType="System.Diagnostics.SourceSwitch"  >
        <listeners>
          <add name="console" 
            type="System.Diagnostics.ConsoleTraceListener" >
            <filter type="System.Diagnostics.EventTypeFilter" 
              initializeData="Error" />
          </add>
          <remove name="Default" />
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="SourceSwitch" value="Warning" />
    </switches>
  </system.diagnostics>
</configuration>

See Also

Reference

Trace and Debug Settings Schema

TraceSource

TraceListener

TraceListener.Filter

TraceFilter