You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Brian Carlson <bc...@Tier.com> on 2010/04/21 20:03:55 UTC

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...

 

<filter
type="MyNameSpace.Logging.Appender.MultiplePropertyFilter,CommonUtilitie
s">

<Condition>

  <key value="Property1" />

  <stringToMatch value="aaaa" />

</Condition>

<Condition>

  <key value="Property2" />

  <stringToMatch value="bbb" />

</Condition>

</filter>

 

 

Does anyone know how to implement this so my class has access to the
nested nodes?

 

Thanks,

Brian


Re: Nested Config settings...

Posted by Ron Grabowski <ro...@yahoo.com>.
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
{
    [TestFixture]
    public class FilterTest
    {
        [Test]
        public void FilterConfigurationTest()
        {
            XmlDocument log4netConfig = new XmlDocument();
            #region Load log4netConfig
            log4netConfig.LoadXml(@"
            <log4net>
            <appender name=""MemoryAppender"" type=""log4net.Appender.MemoryAppender, log4net"">
                <filter type=""log4net.Tests.Filter.MultiplePropertyFilter, log4net.Tests"">
                    <condition>
                        <key value=""ABC"" />
                        <stringToMatch value=""123"" />
                    </condition>
                    <condition>
                        <key value=""DEF"" />
                        <stringToMatch value=""456"" />
                    </condition>
                </filter>
            </appender>
            <root>
                <level value=""ALL"" />
                <appender-ref ref=""MemoryAppender"" />
            </root>
            </log4net>");
            #endregion

            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");
            Assert.IsNotNull(appender);

            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)
        {
            _conditions.Add(condition);
        }
        
        public class Condition
        {
            public string Key { get; set; }
            public string StringToMatch { get; set; }
        }
    }
}
#endif



________________________________
From: Brian Carlson <bc...@Tier.com>
To: Log4NET User <lo...@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…
 
<filter
type="MyNameSpace.Logging.Appender.MultiplePropertyFilter,CommonUtilities">
<Condition>
 
<key value=”Property1” />
 
<stringToMatch value=”aaaa” />
</Condition>
<Condition>
 
<key value=”Property2” />
 
<stringToMatch value=”bbb” />
</Condition>
</filter>
 
 
Does anyone know how to implement this so my class has
access to the nested nodes?
 
Thanks,
Brian