Custom Log Filters and Enterprise Library 5 Configuration Utility

Topics: Building and extending application blocks, Logging Application Block
Jun 2, 2011 at 7:50 PM

I'm in the process of creating a custom log filter capable of filtering based on extended properties. The aim is to get a finer grained logging filter. I have built the class (see code below) and the dll compiles however when I try to configure the filter using the Entlib 5 Configuration utility. It simply doesn't recognize the the filter. I go through the steps of Opening the Web.Config with the EL5  Configuration tool from within VS2010, then >Add Loging Filter > Add Custom Logging Filter. The box with the title "Browsing for Types that derive from ILogFilter..." appears there are no loaded assemblies so I try to load from file - click the button and browse to the Dll with the class defined and click open. After a pause nothing happens... The assembly isn't loaded and my filter isn't there to choose. If I edit the config by hand and add the type into the logfilter section and then open the web.config with Configuration tool then the filter is there to be configured and saves ok.

There seems to be little on this subject and the little that I've read would lead me to believe that it should all work, I've tried restarting VS after building the customFilter project as suggested in another thread. I've looked at the code and it seems to meet all requirements. The project is targetting .Net4 as is the whole solution So I'm wonderring if that could be the issue. I don't know that the filter works, loads etc as configuration seems to be the first hurdle to get over.

Any Ideas?

 Thanks In Anticipation

[ConfigurationElementType(typeof(CustomLogFilterData))]
    public class PropertiesFilter : LogFilter
    {
        private readonly NameValueCollection filterValues;
        private readonly bool hasCategory = false;
        private readonly string category = string.Empty;
        public PropertiesFilter(string name): base(name)
        {
            filterValues = new NameValueCollection();
        }

        public PropertiesFilter(NameValueCollection nvPairs) : this("PropertiesFilter")
        {
            if(nvPairs.Count > 0)
            {
                filterValues = nvPairs;

                if (filterValues.AllKeys.Contains("Category",   StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture,true)))
                {
                    hasCategory = true;
                    category = filterValues.Get("Category");
                }

            }
        }
        public override bool Filter(LogEntry log)
        {
            bool retVal = true;
            IDictionary<string, string> filters = filterValues.ToDictionary();
            var properties  =  log.ExtendedProperties.AsEnumerable().ToDictionary(x => x.Key, x => x.Value.ToString()) ;
                                                                                               
            if (hasCategory == false || log.Categories.Contains(category, StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true)))
            {
                var matches = from p in properties join fv in filters on new {p.Key , p.Value } equals new {fv.Key, fv.Value} select fv;
                retVal = matches.Count() <= 0;
            }
            return retVal;
        }
    } 


Jun 3, 2011 at 4:22 AM

Hi,

I tried to load from file the compiled code you've posted and it loads the custom log filter as expected. Can you try copying the dll to the location where the config editor resides? (usually C:\Program Files\Microsoft Enterprise Library 5.0\Bin). Open the EntLibConfig.NET4.exe, add a logging block then try adding a custom logging filter. The assembly should be recognized and should display your custom logging filter under the loaded assemblies.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 6, 2011 at 10:44 AM

Hi,

I created a new project just with the class provided as above plus a second extension class for the ToDictionary method.  I built the project and place the dll in the C:\Program Files\Microsoft\Enterprise Library 5.0\Bin directory However when I opened EntLibConfigNet4 and followed the steps as suggested the custom filter wasn't displayed. I tried build the dll with and without namespaces and for .net3.5 and .net4 targets. I tried shortening the name of the dll all to no avail. The filter did not display. I also tried the dll on another machine with another installation of entlib 5.  What I did notice in all of this testing was that the config tool was holding a reference to the dll - This I found out because as I was replacing the dll with various different versions the paste would fail because "Another program is using the dll" and when I closed the Config tool the paste succeeded.  As far as I can tell the config tool was aquired a reference to the dll as soon as the tool loaded.  And although the loaded assemblies box is empty the loaded assemblies tree root shows "-" in the Win Server 2003  or expanded arrow in a Vista 32 environment, and in either case you can expand and collapse the branch although nothing is diplayed.

So either I must be missing a step when I build the dll / class - Namespace Specific? or there is something wrong with my Entlib installation (both of them) which means it can't display the tree properly.

Any further thoughts?

Thanks In Anticipation

Jun 7, 2011 at 2:24 AM

Unfortunately its hard to tell what could probably the cause of your problem given that we're unable to reproduce this from our end. One question is, does the CustomFilter class compiles and build successfully? (I know this is pretty obvious since you already have the output DLL but just wanted to check :o)).

If its me, I'd probably try to create maybe a simple console app and try my custom filter if it still doesn't work. If still doesn't, I'd go with reinstalling Entlib just what you have mentioned. But I really have no any assurance if any of this step will work. And the worst case maybe is to manually configure the configuration file to hook up the Custom Filter and see if it will work. HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 20, 2011 at 1:07 PM

You have to sign your project. After that, you'll be able to load your dll in the configuration tool.