Custom Severity Based Filter By Category

Topics: Logging Application Block
Jan 24, 2009 at 4:14 AM
Edited Jan 24, 2009 at 4:18 AM
I needed a new filter to meet the following requirements:
1) Provide "ShouldLog" functionality based off of severity.  ShouldLog only tells you if the log passed the configured filters.  It does not look at the filters that can be assigned to specific tracelisteners.
2) Assign a different severity filter per category.
      Example: This gives you the ability to have the "UserInterface" category log only critical issues but have the "DataAccesLayer" category log verbose.

I searched google for a long time and could not find anything that met these requirements.  So I wrote one.  (c:   Let me know if you guys have any thoughts or suggestions:

I also want to give a shout out to David Douglass who provided me with everything I needed to create a custom filter: David's Blog.

-----Attributes Collection Example-----
Name: UserInterface
Value: Critical

Name: DataAccessLayer
Value: Verbose

    public class SeverityFilter : LogFilter
        #region Member Variables
        private NameValueCollection _attributes = new NameValueCollection();

        #region Constructors
        public SeverityFilter(string Name) : base(Name) { }
        public SeverityFilter(NameValueCollection nameValueCollection) : this("SeverityFilter")
            _attributes = nameValueCollection;

        #region Public Methods
        /// <summary>
        /// Test to see if a message meets the criteria to be processed.
        /// </summary>
        /// <param name="log">Log entry to test.</param>
        /// <returns>
        ///     <b>true</b> if the message passes through the filter and should be logged, <b>false</b> otherwise.
        /// </returns>
        public override bool Filter(LogEntry log)
            //Loop through all the categories and if a match is found
            //determine if the message should be logged
            for (int i = 0; i < log.CategoriesStrings.Length; i++)
                string severity = _attributes.Get(log.CategoriesStrings[i]);
                if (!String.IsNullOrEmpty(severity))
                    TraceEventType allowedSeverity = (TraceEventType)Enum.Parse(typeof(TraceEventType),severity);
                    return ShouldLog(allowedSeverity, log.Severity);

            return true;

        #region Private Methods
        private bool ShouldLog(TraceEventType allowedSeverity, TraceEventType logSeverity)
            //The TraceEventTypes have integer representations that get higher the
            //lower criticality.  Critical=1 Verbose=16.
            if (Convert.ToInt32(allowedSeverity) >= Convert.ToInt32(logSeverity))
                return true;
                return false;

Jan 26, 2009 at 7:52 AM

I dont see something wrong with your code, and if that does suit your need you're good to go. But, my thought on this is that you must understand first which gets logged for the specified severity, for example. "DataAccessLayer" category, logs not only the entries with verbose severity but also other with higher severity. Please see this:

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.