log4net.Filter.PropertyFilter is not working

972 views Asked by At

I am new to logenteries working on log4net filter i have tried remaining filters like "level range,stringtomatch,LoggerMatchFilter" all are working fine however property filter is not working . I have tried different solutions also it doesn't work.

Here is my code :

code:

public class CustomFilter : FilterSkeleton
{
    private bool acceptOnMatch;
    private readonly IList<IFilter> filters = new List<IFilter>();

    public override FilterDecision Decide(LoggingEvent loggingEvent)
    {
        if (loggingEvent == null)
            throw new ArgumentNullException("loggingEvent");

        foreach (IFilter filter in filters)
        {
            if (filter.Decide(loggingEvent) != FilterDecision.Accept)
                return FilterDecision.Neutral; // one of the filter has failed
        }

        // All conditions are true
        if (acceptOnMatch)
            return FilterDecision.Accept;
        else
            return FilterDecision.Deny;
    }

    public IFilter Filter
    {
        set { filters.Add(value); }
    }

    public bool AcceptOnMatch
    {
        get { return acceptOnMatch; }
        set { acceptOnMatch = value; }
    }

}

I have seen this solution in log4net filter - how to write AND filter to ignore log messages but for me filters not adding to interface . i didn't get whether filters are added directly or not ? In loggingevent i am passing the data in message property but the i don't know to how to add the into properties can any one please guide me and i don't know how to call this function.

Webconfig :

   <filter type="Namespace.CustomFilter, Assembly">
    <filter type="log4net.Filter.PropertyFilter">
      <key value="firstname" />
      <stringToMatch value="1234" />
    </filter>
    <filter type="log4net.Filter.PropertyFilter">
      <key value="address" />
      <stringToMatch value="abcdf" />
    </filter>
    <acceptOnMatch value="false" />
  </filter>

I have missed some where please help me out from it . I am struggling from past few hours.

Update:

    public string logFilter()
     {
        log4net.Config.XmlConfigurator.Configure();

        ILog Log = LogManager.GetLogger(
            System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
           );

        var ReferenceID = Guid.NewGuid().ToString();

        var firstname = "1234";
        var address ="abcdf";
        var sno = "12";

        Log.InfoFormat("firstname {0}-{1}", firstname, ReferenceID);

        Log.InfoFormat("address {0}-{1}", address, ReferenceID);

        Log.InfoFormat("sno {0}-{1}", sno, ReferenceID);

        return ReferenceID;
    }

Here decide() function overide from filterskelton but property filter is not applying it able to read the details from webconfig but it can't filter the log message is there anything which i missed in it.

Thanks in advance!

1

There are 1 answers

5
Z.R.T. On

if one of the filters returns FilterDecision.Neutral then log message will be logged. Therefor you need to check that message goes through all filters together

            public override FilterDecision Decide(LoggingEvent loggingEvent)
            {
                if (loggingEvent == null)
                    throw new ArgumentNullException("loggingEvent");

                if (filters.All(x => x.Decide(loggingEvent) != FilterDecision.Accept))
                {
                    return FilterDecision.Neutral;
                }

                // All conditions are true
                if (acceptOnMatch)
                    return FilterDecision.Accept;
                else
                    return FilterDecision.Deny;
            }

UPDATE: my log4net config looks like that :

 <filter type="MyApp.CustomFilter, MyApp">
            <filter type="log4net.Filter.PropertyFilter">
              <key value="firstname" />
              <stringToMatch value="1234" />
            </filter>
            <filter type="log4net.Filter.PropertyFilter">
              <key value="address" />
              <stringToMatch value="abcdf" />
            </filter>
            <acceptOnMatch value="false"/>
          </filter>
        </appender>

the sample how to check logging

    Task.Run(() =>
    {
        log4net.ThreadContext.Properties["firstname"] = "123";
        Log.Info("123");
    });

    Task.Run(() =>
    {
        log4net.ThreadContext.Properties["firstname"] = "1234";
        Log.Info("1234");
    });

    Task.Run(() =>
    {
        log4net.ThreadContext.Properties["address"] = "abcdf";
        Log.Info("abcdf");
    });