Log4Net configuration - LevelMatchFilter
Asked Answered
L

4

19

As there are following levels in log4net

  • ALL
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL
  • OFF

Can anyone please tell me in log4net how can I log only INFO & ERROR or DEBUG & ERROR? What I mean here is be able to log two different levels which are not in sequence?

I can't do if I use following because warning messages will be logged as well which I don't want:

<filter type="log4net.Filter.LevelRangeFilter">
    <acceptOnMatch value="true" />
    <levelMin value="INFO" />
    <levelMax value="ERROR" />
</filter>
Leninism answered 11/10, 2011 at 5:46 Comment(1)
possible duplicate of Discarding several log levels within a range with log4netHaroldharolda
L
32

Use LevelMatchFilter in conjunction with DenyAllFilter:

<filter type="log4net.Filter.LevelMatchFilter">
  <acceptOnMatch value="true" />
  <levelToMatch  value="INFO" />
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
  <acceptOnMatch value="true" />
  <levelToMatch  value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
Leninism answered 17/10, 2011 at 4:15 Comment(3)
Thanks for the example. Just FYI, true is the default value for acceptOnMatch so this will work just as well with those lines elided.Secunda
@Leninism This doesn't work form e at all with the newer version of log4net.Cocytus
What no one has said yet is that the <filter> tags (as shown above) must appear within an <appender> tag! See my additional answer below.Gensmer
G
5

What no one has said yet is that the <filter> tags (as shown in the examples above) must appear within an <appender> tag! For example (this is not a valid log4net configuration--I'm only showing how the <filter> tag is a child entry under <appender>:

<configuration>
  <configSections>
    <section ...="" />
  </configSections>
  <startup>
    <supportedRuntime ...="" />
  </startup>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyyy HH:mm:ss} {%thread} %level - %message%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelMatchFilter">
        <acceptOnMatch value="true" />
        <levelToMatch  value="INFO" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    <root>
      <appender-ref ...="" />
    </root>
    <logger name="Log4NetTest.OtherClass">
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
    </logger>
  </log4net>
</configuration>
Gensmer answered 23/6, 2016 at 14:15 Comment(0)
B
3

@BradLaney et al for whom @iffi's fix is not working , make sure that the version is atleast 1.2.11. Here is the bug report: https://issues.apache.org/jira/browse/LOG4NET-137

Also if you set lossy as true, you would need an evaluator which LevelMatchFilter is not (it's a filter). So this works for me:

    <filter type="log4net.Filter.LevelMatchFilter">
      <param name="LevelToMatch" value="WARN" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />s a

Won't let me add as a comment.

Bonis answered 27/1, 2015 at 14:43 Comment(0)
N
0

try:

<filter type="log4net.Filter.LevelRangeFilter">
    <levelToMatch value="INFO" />
    <acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.LevelRangeFilter">
    <levelToMatch value="ERROR" />
    <acceptOnMatch value="true" />
</filter>
Northing answered 11/10, 2011 at 5:55 Comment(1)
Thanks Mike. I tried as you said, it didn't work. Then I did bit more research along those lines and found solution. See solution in answer section. Thanks for the help.Leninism

© 2022 - 2024 — McMap. All rights reserved.