log4net hierarchy and logging levels

2019-01-21 01:34发布

This site says

Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing priority:

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

DEBUG seems to have lowest priority and ERROR is higher.

Question

  • If I set Min and Max example DEBUG and ERROR it prints everthing DEBUG, INFO, WARN and ERROR. Without use of min and max filter. If I specify ERROR (Logging level = ERROR) Will it include DEBUG, INFO & WARN
 <filter type="log4net.Filter.LevelRangeFilter">
     <param name="LevelMin" value="ERROR"/>
     <param name="LevelMax" value="ERROR"/>
 </filter>

Instead of min and max filter. Is it possible to configure a level and include all other levels below it for logging.

Example - Set level as Error it will include DEBUG, INFO, WARN and ERROR. Is this possible with log4net?

Posting log4net config based on one of comments:

     <?xml version="1.0" encoding="utf-8" ?>
     <configuration>
        <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
        </configSections >
        <log4net debug="true">
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
        <layout type="log4net.Layout.XMLLayout" /> -->
        <param name="File" value="TestLog.log" />
        <param name="AppendToFile" value="false" />
        <layout type="log4net.Layout.PatternLayout">
            <header type="log4net.Util.PatternString" value="[START LOG] %newline" />
            <footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
            <conversionPattern value="%d [%t] %-5p - %m%n" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG"/>
            <param name="LevelMax" value="ERROR"/>
        </filter>
    </appender>
<root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
    </root>
    <logger name="log4NetExample">
        <!-- <appender-ref ref="B" /> -->
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
    </logger>
</log4net>

7条回答
ゆ 、 Hurt°
2楼-- · 2019-01-21 01:56

As others have noted, it is usually preferable to specify a minimum logging level to log that level and any others more severe than it. It seems like you are just thinking about the logging levels backwards.

However, if you want more fine-grained control over logging individual levels, you can tell log4net to log only one or more specific levels using the following syntax:

<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="WARN"/>
</filter>

Or to exclude a specific logging level by adding a "deny" node to the filter.

You can stack multiple filters together to specify multiple levels. For instance, if you wanted only WARN and FATAL levels. If the levels you wanted were consecutive, then the LevelRangeFilter is more appropriate.

Reference Doc: log4net.Filter.LevelMatchFilter

If the other answers haven't given you enough information, hopefully this will help you get what you want out of log4net.

查看更多
太酷不给撩
3楼-- · 2019-01-21 01:57

DEBUG will show all messages, INFO all besides DEBUG messages, and so on.
Usually one uses either INFO or WARN. This dependens on the company policy.

查看更多
\"骚年 ilove
4楼-- · 2019-01-21 02:01

For most applications you would like to set a minimum level but not a maximum level.

For example, when debugging your code set the minimum level to DEBUG, and in production set it to WARN.

查看更多
Anthone
5楼-- · 2019-01-21 02:10

Try like this, it worked for me

<root>
  <!--<level value="ALL" />-->
  <level value="ERROR" />
  <level value="INFO" />
  <level value="WARN" />     
</root>

This logs 3 types of errors - error, info, and warning

查看更多
混吃等死
6楼-- · 2019-01-21 02:17

This might help to understand what is recorded at what level Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing severity - Log Level.

Number of levels recorded for each setting level:

 ALL    DEBUG   INFO    WARN    ERROR   FATAL   OFF
•All                        
•DEBUG  •DEBUG                  
•INFO   •INFO   •INFO               
•WARN   •WARN   •WARN   •WARN           
•ERROR  •ERROR  •ERROR  •ERROR  •ERROR      
•FATAL  •FATAL  •FATAL  •FATAL  •FATAL  •FATAL  
•OFF    •OFF    •OFF    •OFF    •OFF    •OFF    •OFF
查看更多
beautiful°
7楼-- · 2019-01-21 02:18

Its true the official documentation (Apache log4net™ Manual - Introduction) states there are the following levels...

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

... but oddly when I view assembly log4net.dll, v1.2.15.0 sealed class log4net.Core.Level I see the following levels defined...

public static readonly Level Alert;
public static readonly Level All;
public static readonly Level Critical;
public static readonly Level Debug;
public static readonly Level Emergency;
public static readonly Level Error;
public static readonly Level Fatal;
public static readonly Level Fine;
public static readonly Level Finer;
public static readonly Level Finest;
public static readonly Level Info;
public static readonly Level Log4Net_Debug;
public static readonly Level Notice;
public static readonly Level Off;
public static readonly Level Severe;
public static readonly Level Trace;
public static readonly Level Verbose;
public static readonly Level Warn;

I have been using TRACE in conjunction with PostSharp OnBoundaryEntry and OnBoundaryExit for a long time. I wonder why these other levels are not in the documentation. Furthermore, what is the true priority of all these levels?

查看更多
登录 后发表回答