Ukadc.Diagnostics.Filters.MultiFilter

The System.Diagnostics namespace in .NET 2.0 (and above) allows you to filter logging 'events' at the listener level. In the configuration example below we specify a ConsoleTraceListener with a filter that stops any events with an event type below Warning and stops them getting to the listener.

<configuration>
  <system.diagnostics>
    <sharedListeners>
      <add name="Console" type="System.Diagnostics.ConsoleTraceListener">
        <filter type="System.Diagnostics.EventTypeFilter" initializeData="Warning" />
      </add>
    </sharedListeners>
  </system.diagnostics>
</configuration>



However, one limitation of this API is that only one filter can be specified for each listener. The MultiFilter provided in the UKAdc.Diagnostics library allows the addition of multiple Filters to a single listener like so:

<configuration>
  <system.diagnostics>
    <sharedListeners>
      <add name="Console" type="System.Diagnostics.ConsoleTraceListener">
        <filter type="Ukadc.Diagnostics.MultiFilter, Ukadc.Diagnostics" initializeData="NameOfFilterGroup" />
      </add>
    </sharedListeners>
  </system.diagnostics>

  <ukAdc.diagnostics>
    <filterGroups>
      <filterGroup name="NameOfFilterGroup" logic="And">
        <filters>
          <filter type="System.Diagnostics.EventTypeFilter" initializeData="Critical" />
          <filter type="System.Diagnostics.SourceFilter" initializeData="NameOfTraceSource" />
        </filters>
      </filterGroup>
    </filterGroups>
  </ukAdc.diagnostics>
</configuration>


Negate

When registering a filter inside a filterGroup it is also possible to specify an additional negate attribute. This defaults to false but when set to true it will have the effect of performing a NOT operation on the result of the Filter.

For example, the SourceFilter provided with System.Diagnostics filters all events that are from any source other than the one specified in the initializeData attribute.

<add name="Console" type="System.Diagnostics.ConsoleTraceListener">
  <filter type="System.Diagnostics.SourceFilter" initializeData="NameOfDesiredSource" />
</add>


In the example given above the filter would prevent any events from any source other than "NameOfDesiredSource" from reaching the ConsoleTraceListener. But what if we wanted to remove all events from a specific source.

We could use the Negate attribute on a filterGroup to invert the result from the filter:

<add name="Console" type="System.Diagnostics.ConsoleTraceListener">
	<filter type="Ukadc.Diagnostics.MultiFilter, Ukadc.Diagnostics" initializeData="filterGroup1" />
</add>

...

<filterGroup name="filterGroup1"
  <filters>
    <filter type="System.Diagnostics.SourceFilter" initializeData="NameOfUndesiredSource" negate="true" />
  </filters>
</filterGroup>



This setup is entirely valid (it is fine to have just one filter in a filter group) and would prevent all events from the TraceSource "NameOfUndesiredSource" from reaching the ConsoleTraceListener.

Last edited Apr 24, 2008 at 8:23 PM by joshtwist, version 18

Comments

No comments yet.