Custom Logging Filter

Topics: Logging Application Block
Apr 5, 2012 at 3:41 AM

Hi,

 

Does anyone have an idea about how to create a custom Logging filter?  Clear explanation will be really appreciated.

 

Thanks

Pavan

Apr 5, 2012 at 3:48 AM

See: How to create a custom logging severity filter? or Using Custom Filters in the Enterprise Library Logging Block.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 5, 2012 at 4:17 AM

Hi Randy,

 

The second link is really good.  I created the same class file that was mentioned on that link.  But, after that how can I configure the web.config.  I tried to open the config file through config tool.  But severity filter is not existing.  Is there anything needs to be done after this?

 

Thanks

Apr 5, 2012 at 4:41 AM
Edited Apr 5, 2012 at 4:46 AM

You can always manually type the configuration information into web.config but this can be cumbersome and error prone.  The config looks like:

        <logFilters>
            <add type="ConsoleApplication22.AdministrativeFilter, ConsoleApplication22, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="AdministrativeFilter" />
        </logFilters>

To use the configuration tool you would:

  1. Click Add Logging Filters->Add Custom Logging Filter
  2. The Type Browser appears
  3. Click Add from File
  4. Browse to the location of the assembly with the custom filter, select it and click Open
  5. The assembly should now appear in the list of loaded assemblies
  6. Drill down in the tree until you see the filter name and click OK
  7. Change the name and add any Key/Value pairs if appropriate

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 5, 2012 at 3:27 PM

I am sorry...its a silly quesiton.  But, can you tell me what is this 4th point in it and what are key/value pairs.  I just want to add a severity filter and severities in my case are going to be error, general, exception...something like that.  Based on these i need to filter the logs.  Can you explain the scenario clearly.  Thanks

Apr 5, 2012 at 3:47 PM

The Config UI lets you add Key/Value pairs.  It sounds like you don't need them so you can ignore that part.  :)

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 5, 2012 at 5:27 PM

Hi Randy,

Unfortunately....it doesn't clear me.  

namespace RatabaseLogging
{
    [ConfigurationElementType(typeof(CustomLogFilterData))]
    class SeverityFilter:LogFilter 
     {
        public SeverityFilter(string Name) : base(Name) {}
        public SeverityFilter(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;
  }
 }
}

This is what the code I copied from the link.  I don't know how to implement this.... so that I can configure something through my config tool.
I am very new to dotnet and also very new to this ent library.  Please reply as I am a fresher...:)  I don't understand the 4th point in one of 
the above reply also....:(

Apr 5, 2012 at 9:37 PM

You configure the filter with the parameters you need.  For example if you needed a parameter called Hello with a value of World:

 

<logFilters>
    <add Hello="World" type="ConsoleApplication22.AdministrativeFilter, ConsoleApplication22, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="AdministrativeFilter" />
</logFilters>

 

Then you just have the Filter pull out the value you need:

 

public AdministrativeFilter(NameValueCollection nvPairs) : this("AdministrativeFilter") 
{
    string helloValue;

    foreach (var key in nvPairs.AllKeys)
    {
        if (string.Compare(key, "Hello", StringComparison.OrdinalIgnoreCase) == 0)
        {
            helloValue = nvPairs[key];
        }
    }
}

Your response makes me ask a few questions.  Is your Custom Filter just for severity?  Enterprise Library already allows categories and trace listeners to have built-in severity filters without the use of a Custom Filter.  Also, your definition of severity ("error, general, exception") doesn't match with the severities that Enterprise Library is already using.  Do you actually intend these to be categories?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

 

Apr 5, 2012 at 11:56 PM

Hi randy,

You are right.  When I see in the tracelisteners....I see some severity filter dropdown.  But when I try to add that in LoggingFilter section... I am not finding severity.  I am just finding, category, priority, enabling and custom log filters.  Thats why I am trying to create a custome log filter for my severity requirement.

 

Please let me know the things that i need to do.

 

Apr 6, 2012 at 7:59 AM

You can just set the "Minimum Severity" on the category.  E.g. if the Minimum Severity is set to Warning then Warning, Error and Critical Severity will be sent to the appropriate Trace Listeners.  If you want control within a category you can use the Severity Filter on individual Trace Listeners.  E.g. within one category you can have a one Trace Listener with the Severity Filter set to All that logs all severity and another Trace Listener that has its Severity Filter set to Error so it will only handle entries that have the Severity set to Error or Critical.

Using those settings you can usually handle most severity filtering unless you have some other specific requirement.  E.g. you want to process Warning messages but not Error or Critical.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com