Search code examples
log4netlog4net-configurationlog4net-filter

Log4net error with XmlHierarchyConfigurator. Missing Filter Property


One of our WebApis stopped outputting logs recently. In the Trace file we were getting this message:

log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [filter] to set object on [log4net.Repository.Hierarchy.Hierarchy]

The config file looks as below and I'm assuming the filter in the error is the same filter in the config so I'm not sure why it can't find it. I upgraded to the latest version of log4net and that solved the issue for a few days and then we got the "Cannot find Property [filter]" error again. The config file hasn't changed at all over this period.

<configuration>
  <configSections>
    <section name="log4net"
        type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <filter type="log4net.Filter.LevelMatchFilter">
      <acceptOnMatch value="true" /> <!--change to false to exclude info logs -->
      <levelToMatch  value="INFO" />
    </filter>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/Log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1001KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level: %message%newline" />
      </layout>
    </appender>
  </log4net>

</configuration>

It's weird that it worked for a while, stopped, updgraded, worked for a while, stopped. I'm at at loss as to what could have changed to make it stop working either time.

Thanks in advance for any help.


Solution

  • Your configuration is not valid.
    A filter can only be defined within an appender.

    The documentation mentions:

    Filters elements may only be defined as children of <appender> elements.

    Move the filter definition to the appender as shown below.

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <filter type="log4net.Filter.LevelMatchFilter">
            <acceptOnMatch value="true" /> <!--change to false to exclude info logs -->
            <levelToMatch  value="INFO" />
        </filter>
        <file value="Logs/Log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="1001KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level: %message%newline" />
          </layout>
    </appender>
    

    (I can't explain why the error only occurs at certain/random times; probably it (re-)occurs at configuration time eg. after an website/appdomain recycle) ...
    In the end, Log4net tries to keep continuing as much as possible.