Using Custom Filters in the Enterprise Library Logging Block

One of the things you can configure in the Enterprise Library logging block is a custom filter.  This is a class that examines log entries prior to them being written and can prevent a log entry from being written.

Microsoft's documentation on this subject is pretty lacking, so here's what you need to be aware of:

1) You need to inherit from LogFilter, even though the documentation says to implement ILogFilter.  LogFilter inherits from ILogFilter.

2) You have to decorate your derivation with a ConfigurationElementType attribute and declare the type (in the attribute, see the code sample below) as CustomLogFilterData.  The class will compile without this, but won't load in the configuration editor.  The message from the configuration editor is misleading, it claims you didn't derive from LogFilter.

3) The base class lacks a parameterless constructor, so, you have to supply a constructor that takes a string and calls the base class's corresponding constructor.

4) Although not part of the contract with the base class/interface, you need to supply a constructor that takes a NameValueCollection.  This contains the parameters that are defined in the configuration editor.  The class will compile without this, but can't be instantiated at runtime without it.

Here's a sample:

using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

namespace LoggingTest {
 [ConfigurationElementType(typeof(CustomLogFilterData))]
 public class AdministrativeFilter : LogFilter {
  public AdministrativeFilter(string Name) : base(Name) {}
  public AdministrativeFilter(NameValueCollection nvPairs) : this("AdministrativeFilter") {}
  public override bool Filter(LogEntry log) {
   if (
    log.Severity == TraceEventType.Critical ||
    log.Severity == TraceEventType.Error ||
    log.Severity == TraceEventType.Start ||
    log.Severity == TraceEventType.Stop ||
    log.Severity == TraceEventType.Warning)
   {
    return true;
   }
   return false;
  }
 }
}

The big problem with all this is the whole filter concept in the logging block.  Filters (custom and otherwise) are global.  Whatever filters you define apply to all destinations.  What you really need (and EIF had) is filters by destination.  This way, you can have a log file filled with tracing detail while only sending the important stuff to the event log.  If you send the detail to the event log, the event log becomes impossible to work with and the volume of messages may overwrite old messages that you need.

Print | posted @ Monday, May 15, 2006 4:36 AM

Comments on this entry:

Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by Sunil Lath at 5/29/2006 4:28 PM

This is brilliant. Its great to have resources like these available on the net. Couldnt find any other documentation that suggest what is required to build a custom filter.
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by David at 6/14/2007 7:46 AM

I wish Microsoft had bothered with useful documentation like this for the Enterprise Library! It's a minefield!!
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by Hank Marvin at 6/14/2007 7:49 AM

Im hungry, whats for dinner?
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by Dave McClelland at 9/10/2007 1:56 PM

I'd like to implement a custom filter based on Severity, just as your example shows. Where do I put this code? What configuration items need to go with it? Is it practical to set the Severity level in the config file?
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by Veni at 10/3/2007 1:45 PM

This is a very helpful post, especially considering there isn't much documentation for creating custom filters with the Logging Application Block. I'm trying to create a custom filter which will filter on emails, to prevent a recipient from getting spammed with emails for the same exception. Can you describe how to configure this custom filter in the web.config?
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by radiodave at 8/11/2008 3:46 AM

thanks, been battling with this one for a few days now. one other thing: seems that the entlib configuration will only load the filter if it exists in an assembly on it's own, i tried creating a custom filter class as part of a separate logging assembly wrapper class and it's didn't like that.
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by max at 10/15/2008 2:44 PM

nice one !!
Gravatar # re: Using Custom Filters in the Enterprise Library Logging Block
by Heng at 12/17/2008 10:08 AM

It seems a DLL which implements a custom filter(s) has to be strong named. Otherwise, the configuration console is not able to load it and give a misleading error message, "There were no types found in the assembly '...' that implement or inherit from the base type 'Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogFilter'.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: