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

北城余情 提交于 2019-12-22 04:24:05

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/24172777/in-log4net-xml-configuration-is-priority-the-same-thing-as-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!