log4net中需要澄清 [英] clarrification needed in log4net

查看:97
本文介绍了log4net中需要澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已使用以下URL来实现log4net,
[ code_project ] ,谁能帮助我理解 第6步 ?

Hello all,

I have used the following URL to implement log4net,
[code_project], Can anyone please help me to understand step 6?

if (log.IsErrorEnabled)
{
    log.Error("Page Load failed : " + ex.Message);
}


我不明白 if(log.IsErrorEnabled)的工作原理...

关于


I don''t understand how if (log.IsErrorEnabled) works...

Regards

推荐答案

IsErrorEnabled的属性检查您的app.config文件,以查看是否已打开该级别的日志记录.您可以像这样打开日志记录,以进行错误日志记录.您还必须安装一个名为EventLogAppender的附加程序.但是可以将级别更改为WARN或INFO或DEBUG,以便仅将其记录在该级别之上.因此,在此示例中,它仅记录错误.如果它使用的是WARN,则检查错误的调用仍将返回true,因为它是一个更高的级别.

The property for IsErrorEnabled checks your app.config file to see if you have that level of logging turned on. You can turn on the logging like this to have error logging on. You would also have to have an appender installed named EventLogAppender. But the level can be changed to just WARN or INFO or DEBUG so that it will only log above that level. So in this example it logs for errors only. If it was using WARN then a call to check for errors would still return true since it is a higher level.

<root>
  <level value="ERROR"/>
  <priority value="ERROR"/>
  <appender-ref ref="EventLogAppender"/>
</root>



如果您在log4net方面需要更多帮助,请告诉我.



If you need more help with log4net, let me know.


是的,史蒂夫,我仍然有疑问..

我在我的web.config文件中添加了以下代码
Yes Steve, I still have doubt..

I have added the following code in my web.config file
<log4net debug="true">
     <appender name="RollingLogFileAppender">
               type="log4net.Appender.RollingFileAppender">
          <file value="C:\\TestProj\\TestLog.txt" />
        <appendtofile value="true" />
        <rollingstyle value="Size" />
        <maxsizerollbackups value="10" />
        <maximumfilesize value="2KB" />
        <staticlogfilename value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionpattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
     </appender>
     <root>
	 <level value="INFO" />
	<appender-ref ref="RollingLogFileAppender" />
     </root>
</log4net>



并在.cs文件中添加了以下代码



And added the following code in the .cs file

ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("This a test debug message");



在这种情况下,文件中不会写入任何内容.从上面的答复中我了解到,即使我的日志级别为 INFO ,它也应该将内容记录到文件中,因为我没有检查任何条件.

我认为上面的代码应该在日志文件中写入内容,而下面的代码则不应,因为要进行检查.



In this case nothing is getting written in the file. What I understood from the above reply is, it should log the content to the file as I am not checking any conditions, even if the log level is INFO.

I think the above code should write something to log file and the following code shouldn''t, as there is a check.

ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    if (log.IsDebugEnabled)
    {
       log.Debug("This a test debug message");
    }



你能帮我吗?

谢谢



Can you help me on this?

Thanks


这篇关于log4net中需要澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆