为什么 IsDebugEnabled 在我的代码中始终为 True?
我有以下代码:
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
XmlConfigurator.Configure(new FileInfo("log4net.config"));
问题是下面的语句总是返回 True
if (Log.IsDebugEnabled) { /* do time consuming stuff and log the result */
这是我的 log4net 配置文件_
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
<param name="File" value="Log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%5p [%d] - %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
为什么 IsDebugEnabled 总是返回 True?
I have the following code:
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
XmlConfigurator.Configure(new FileInfo("log4net.config"));
The problem is the statement below always returns True
if (Log.IsDebugEnabled) { /* do time consuming stuff and log the result */
This is my log4net config file_
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
<param name="File" value="Log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%5p [%d] - %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
Why does IsDebugEnabled always return True?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 log4net 手册:
尝试:
请注意,
IsDebugEnabled
由记录器级别控制。由于记录器和附加器之间的松散耦合,所有附加器上都有 LevelMin/LevelMax 过滤器这一事实并没有帮助。From the log4net manual:
Try:
Note that
IsDebugEnabled
is controlled by the logger level. The fact that you have LevelMin/LevelMax filters on all your appenders doesn't help, because of the loose coupling between loggers and appenders.为 RollingFileAppender 定义 rootCategory
define rootCategory for RollingFileAppender
您需要
在调用
LogManager.GetLogger
方法之前调用。我建议您在应用程序启动时执行此操作。因为按照现在的代码方式,在您实例化记录器之前,它不会配置记录器。You need to call
before you call the
LogManager.GetLogger
method. I would recommend doing this in startup of your application. Because with the code the way it is now, it is not configuring the logger until after you have already instantiated it.这是因为未设置根级别值。
默认情况下,它将采用所有级别。
通过将根级别值定义为
INFO
、WARN
、ERROR
和FATAL
将解决您的问题。That's because the Root Level Value is not been Set.
By Default, it will take all the Levels.
By defining the Root Level Value to
INFO
,WARN
,ERROR
,andFATAL
will solve your issue.