Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Log4Net XML configuration is Priority the same thing as Level?

I inherited some code that uses the priority element under the root in its xml configuraiton. This is just like the example at http://iserialized.com/log4net-for-noobs/ which shows:

<root>
 <priority value="ALL" />
 <appender-ref ref="LogFileAppender" />
 <appender-ref ref="ConsoleAppender"/>
</root>

However, the log4net configuration examples at http://logging.apache.org/log4net/release/manual/configuration.html always show it using the level element:

<root>
 <level value="DEBUG" />
 <appender-ref ref="A1" />
</root>

In this type of configuration is

<priority>

the same as

<level>

?

Can someone point me to somewhere in the docs where this is explained?

like image 305
Michael Levy Avatar asked Jun 11 '14 21:06

Michael Levy


People also ask

What is level value in log4net?

log4net offers the following log levels, in increasing order of priority: ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The ALL level logs everything and the OFF level logs nothing.

What are the main components of log4net?

Log4net has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

What is log4net config?

The log4net configuration can be configured using assembly-level attributes rather than specified programmatically. If specified, this is the filename of the configuration file to use with the XmlConfigurator. This file path is relative to the application base directory (AppDomain. CurrentDomain.

Where are log4net logs stored?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation. The different log files are described in Services logs.


1 Answers

There is no Priority property on the Logger class in log4net. The only instance of Priority I could find was in the SmtpAppender. So into the code I went!

In the ParseChildrenOfLoggerElement method of the XmlHierarchyConfigurator you will find the following code:

if (xmlElement.LocalName == "level" || xmlElement.LocalName == "priority")
{
    this.ParseLevel(xmlElement, log, isRoot);
}

Ah! Both values are resolved to the same property (the ParseLevel method doesn't really do much, apart from assignation, logging and management of the "inherited" value which is a possible level) so there you have it; "level" and "priority" have the exact same effect on your configuration. I guess this was done to keep some kind of backward compatibility with a previous version of the library, which is in fact supported by this article about log4j:

In early versions of log4j, these were called category and priority, but now they're called logger and level, respectively.

Indeed, if we search for "category", there is a Configure method in the XmlHierarchyConfigurator which contains the following code:

// ...
XmlElement xmlElement = (XmlElement)xmlNode;
if (xmlElement.LocalName == "logger")
{
    this.ParseLogger(xmlElement);
}
else
{
    if (xmlElement.LocalName == "category")
    {
        this.ParseLogger(xmlElement);
    }
    else
    {
        if (xmlElement.LocalName == "root")
        {
            this.ParseRoot(xmlElement);
        }
    // ...

So that's it: level and priority are interchangeable, as are logger and category.

Interesting tidbit: the last property wins, and there is no limit regarding the number of properties you could have on the logger, so this is valid and would set the level to DEBUG

<root>
 <priority value="ALL" />
 <priority value="ERROR" />
 <level value="DEBUG" />
</root>
like image 54
samy Avatar answered Sep 18 '22 20:09

samy