我有以下代码:
public void LoggingAI(Exception ex)
{
var log = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
XmlConfigurator.Configure();
if (log.IsInfoEnabled)
{
log.Error(ex);
}
}
我在web.config中放置了以下代码
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="Info"/>
</root>
</log4net>
我不明白为什么log.IsInfoEnabled
,log.IsErrorEnabled
等所有属性都都是false
。
我在这里想念东西吗?
答案 0 :(得分:1)
在Log4Net中使用任何东西之前,请调用Configure()。
无法重现您的情况:
首先将以下内容添加到“配置”部分:
<configSections>
<!--- Other Stuff here --->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
下面是Log4Net示例Config部分:
<log4net>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="INFO" />
<foreColor value="Green, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Cyan, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="FATAL" />
<foreColor value="Yellow, HighIntensity" />
<backColor value="Red" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %class.%method%newline%message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>