How to view a list of configuration properties in a custom log4net filter

I am creating a custom filter in log4net. I found that I can map the elements in the config to the properties in the filter class as follows:

<filter type="ConsoleApplication1.CustomFilter">
    <FooKey value="FooValue"/>
</filter>

public class CustomFilter : FilterSkeleton
{
    public string FooKey { get; set; }

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

What I would like to do is set up a list of these elements:

<filter type="ConsoleApplication1.CustomFilter">
    <FooKey value="FooValue"/>
    <FooKey value="BarValue"/>
</filter>

public class CustomFilter : FilterSkeleton
{
    public string[] FooKeys { get; set; }

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

If possible, how would I do it?

+4
source share
1 answer

You can find the code responsible for this behavior in the method SetParameter XmlHierarchyConfigurator. What happens is:

  • If the xml element refers to a property, then it will be assigned
  • If the xml element refers to a method, the method will be called using the xml parameter

*.config, . , :

public class CustomFilter : FilterSkeleton
{
    public string[] FooKeys { get; set; }

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

    public void AddFooKey(string text)
    {
        var temporaryFooKey = new List<string>(FooKeys ?? new List<string>().ToArray());
        temporaryFooKey.Add(text);
        FooKeys = temporaryFooKey.ToArray();
    }
}

:

<filter type="ConsoleApplication1.Filters.CustomFilter">
  <FooKey name="AddFooKey" value="FooValue"/>
  <AddFooKey value="BarValue"/>
</filter>

, xml, name, , , .

+5

Source: https://habr.com/ru/post/1534890/


All Articles