Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

c# - log4net hierarchy and logging levels

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>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...