logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Nested Config settings...
Date Fri, 23 Apr 2010 03:36:02 GMT
I'll commit this to svn too.

#if NET_2_0
using System;
using System.Collections.Generic;
using System.Xml;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Filter;
using log4net.Repository;
using NUnit.Framework;

namespace log4net.Tests.Filter
    public class FilterTest
        public void FilterConfigurationTest()
            XmlDocument log4netConfig = new XmlDocument();
            #region Load log4netConfig
            <appender name=""MemoryAppender"" type=""log4net.Appender.MemoryAppender, log4net"">
                <filter type=""log4net.Tests.Filter.MultiplePropertyFilter, log4net.Tests"">
                        <key value=""ABC"" />
                        <stringToMatch value=""123"" />
                        <key value=""DEF"" />
                        <stringToMatch value=""456"" />
                <level value=""ALL"" />
                <appender-ref ref=""MemoryAppender"" />

            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
            XmlConfigurator.Configure(rep, log4netConfig["log4net"]);

            IAppender[] appenders = LogManager.GetRepository(rep.Name).GetAppenders();
            Assert.IsTrue(appenders.Length == 1);

            IAppender appender = Array.Find(appenders, a => a.Name == "MemoryAppender");

            MultiplePropertyFilter multiplePropertyFilter = 
                ((AppenderSkeleton)appender).FilterHead as MultiplePropertyFilter;

            var conditions = multiplePropertyFilter.GetConditions();
            Assert.AreEqual(2, conditions.Length);
            Assert.AreEqual("ABC", conditions[0].Key);
            Assert.AreEqual("123", conditions[0].StringToMatch);
            Assert.AreEqual("DEF", conditions[1].Key);
            Assert.AreEqual("456", conditions[1].StringToMatch);

    public class MultiplePropertyFilter : FilterSkeleton
        private readonly List<Condition> _conditions = new List<Condition>();

        public override FilterDecision Decide(LoggingEvent loggingEvent)
            return FilterDecision.Accept;

        public Condition[] GetConditions()
            return _conditions.ToArray();

        public void AddCondition(Condition condition)
        public class Condition
            public string Key { get; set; }
            public string StringToMatch { get; set; }

From: Brian Carlson <bcarlson@Tier.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Wed, April 21, 2010 2:03:55 PM
Subject: Nested Config settings...

I have a custom filter, doing an AND of multiple property
checks.  I need to be able to read in the following config…
<key value=”Property1” />
<stringToMatch value=”aaaa” />
<key value=”Property2” />
<stringToMatch value=”bbb” />
Does anyone know how to implement this so my class has
access to the nested nodes?
View raw message