145

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>

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Siva
  • 2,791
  • 5
  • 29
  • 33
  • have you tried to set logging level to INFO and checked if it includes INFO and DEBUG? – default Jan 19 '12 at 13:01
  • 5
    _DEBUG seems to have lowest priority and Error is highest._ This not a **priority**, but a **threshold** up or above a message will be logged. – R. Schreurs Jul 12 '13 at 09:23
  • 1
    Have you tried only using `levelMax`? I think that that should include everything below it if you don't specify a `levelMin` – A N Sep 28 '15 at 18:05

9 Answers9

129

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
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
Mel Pama
  • 1,411
  • 1
  • 9
  • 8
38

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.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
19

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.

weismat
  • 7,195
  • 3
  • 43
  • 58
  • Please check my updated question, My question is for levels and log statements – Siva Jan 19 '12 at 13:23
  • Please post your whole log4Net config, you might have two loggers which are interfering. – weismat Jan 19 '12 at 13:25
  • And I have personally used only the level setting so far. I think Min/Max settings should be avoided if possible. – weismat Jan 19 '12 at 13:27
  • I have posted whole log4net config. Please check. I am trying to learn and implement. – Siva Jan 19 '12 at 13:29
18

Here is some code telling about priority of all log4net levels:

TraceLevel(Level.All); //-2147483648

TraceLevel(Level.Verbose);   //  10 000
TraceLevel(Level.Finest);    //  10 000
    
TraceLevel(Level.Trace);     //  20 000
TraceLevel(Level.Finer);     //  20 000
    
TraceLevel(Level.Debug);     //  30 000
TraceLevel(Level.Fine);      //  30 000
    
TraceLevel(Level.Info);      //  40 000
TraceLevel(Level.Notice);    //  50 000
    
TraceLevel(Level.Warn);      //  60 000
TraceLevel(Level.Error);     //  70 000
TraceLevel(Level.Severe);    //  80 000
TraceLevel(Level.Critical);  //  90 000
TraceLevel(Level.Alert);     // 100 000
TraceLevel(Level.Fatal);     // 110 000
TraceLevel(Level.Emergency); // 120 000
    
TraceLevel(Level.Off); //2147483647


private static void TraceLevel(log4net.Core.Level level)
{
   Debug.WriteLine("{0} = {1}", level, level.Value);
}

dev.doc
  • 569
  • 3
  • 12
  • 18
10

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.

christutty
  • 952
  • 5
  • 12
ulty4life
  • 2,972
  • 1
  • 25
  • 31
  • ah the deny is what i, personally, was looking for. would like info+error messages but not debug. thanks for this. – Sarfaraaz Nov 01 '17 at 11:50
  • `by adding a "deny" node to the filter` how? – mrhotroad Jul 24 '19 at 06:16
  • @mrhotroad I'm not sure how different the current version of log4net is from the version in 2014, but here's the doc on LevelMatchFilter.AcceptOnMatch. http://logging.apache.org/log4net/release/sdk/html/P_log4net_Filter_LevelMatchFilter_AcceptOnMatch.htm So you can do – ulty4life Jul 25 '19 at 20:03
8

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?

barrypicker
  • 9,740
  • 11
  • 65
  • 79
  • 2
    For what it's worth, they're all listed at the following link, but they haven't updated their manuals in a while it seems. http://logging.apache.org/log4net/release/sdk/html/T_log4net_Core_Level.htm – Dinerdo Mar 27 '17 at 15:08
  • 2
    Level Fine; Level Finer; Level Finest; I'm just going to use these three from now on. – maembe May 20 '19 at 18:55
0

you can use this for certain log warn: (add to appender)

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

and if you want log error to alert range:

<filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="ALERT" />
</filter>
M Safari
  • 1
  • 2
0

Set the log level in <root> where the logging will then include that level and all other levels below it:

<root>
  <level value="DEBUG" />
  <appender-ref ref="TraceAppender" />
  <appender-ref ref="ConsoleAppender" />
  <appender-ref ref="RollingFileAppender" />
</root>
user8128167
  • 6,929
  • 6
  • 66
  • 79
-5

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

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Dhananjay
  • 3,673
  • 2
  • 22
  • 20
  • 8
    According to [log4net docs](https://logging.apache.org/log4net/release/manual/configuration.html#root) for Root Logger: "level: Optional element, **maximum of one allowed**. Defines the logging level for this logger. This logger will only accept event that are **at this level or above.**" – gonadarian Jun 14 '18 at 10:49
  • Why this answer has given down vote ?. It's working for me also. Hence up voting. – amilamad Mar 27 '19 at 02:29
  • 2
    Because would have sufficed. – rlesias Apr 17 '19 at 14:23