使用log4net的正确方式(记录器命名) [英] Correct way of using log4net (logger naming)

查看:172
本文介绍了使用log4net的正确方式(记录器命名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有配置和使用log4net的两种方式。第一个是,当我可以配置我自己的追加程序和相关记录:

 <  - 语言:XML  - > 

<追加程序名称=myLogAppenderTYPE =log4net.Appender.RollingFileAppender>
<文件值=Logs\myLog.log/>
<布局类型=log4net.Layout.PatternLayout>
< conversionPattern值=%DATE%的水平 - %讯息%N/>
< /布局>
< /附加器>

<记录器名称=myLog>
<电平值=全部>< /水平>
<附加目的地-REF REF =myLogAppender/>
< /记录器>



,然后当我想要写日志的东西,我可以做到以下几点:

  ILog的日志= LogManager.GetLogger(myLog); 
log.Info(信息);

要使用它的另一种方法是配置根要尽可能详细,我想:



 <  - 语言:XML  - > 

<根和GT;
<电平值=错误/>
<附加目的地-REF REF =myLogAppender/>
< /根>



在这种情况下我可以登录的消息是这样的:

  ILog的日志= LogManager.GetLogger(typeof运算(酒吧)); 
log.Info(信息);



第二方法的好处是,你可以启用或禁用对飞一些消息。但问题是,我在EPiServer CMS正在开发,它具有使用log4net的自己的日志记录系统,如果我能在信息根级别日志记录,那么大量的系统日志将被写入。



如何使用log4net的?系统的每个部分在其自己的记录写入,或者一切都写在默认的日志记录器和配置决定下一步该怎么做?


解决方案

至于怎么在代码日志消息,我会选择第二种方式:

  ILog的日志= LogManager.GetLogger(typeof运算(酒吧)); 
log.Info(信息);



有前款发送到日志将被命名为使用完全组队参加类型的消息酒吧,如:

  MyNamespace.Foo.Bar [INFO]消息

这种方法的优点是,它是组织采伐的事实标准,它也可以让你筛选通过命名空间日志消息。例如,您可以指定要记录INFO级别的消息,但筹集的日志记录级别酒吧专为DEBUG:



<预类=郎咸平的XML prettyprint-覆盖> <&log4net的GT;
<! - 追加程序去这里 - >
<根和GT;
<电平值=INFO/>
<附加目的地-REF REF =myLogAppender/>
< /根>

<记录器名称=MyNamespace.Foo.Bar>
<电平值=DEBUG/>
< /记录器>
< / log4net的>



通过名称来过滤日志的能力是log4net的一个强大的功能,如果你只需登录你所有的消息myLog,你失去了很多这种权力!



关于EPiServer CMS,你应该能够,使用上述方法来指定CMS和您自己的代码不同的日志记录级别



有关进一步的阅读,这里是一个CodeProject上的文章中,我在日志中写道:




There are two ways of configuring and using log4net. First one is when I can configure my own appender and associated logger:

<!-- language: xml -->

<appender name="myLogAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="Logs\myLog.log" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level - %message%n" />
    </layout>
</appender>

<logger name="myLog">
    <level value="All"></level>
    <appender-ref ref="myLogAppender" />
</logger>

And then when I want to write something in log, I can do the following:

ILog log = LogManager.GetLogger("myLog");
log.Info("message");

Another way to use it is to configure root to be as detailed as I want:

<!-- language: xml -->

<root>
    <level value="Error" />
    <appender-ref ref="myLogAppender" />
</root>

And in this case I can log messages like this:

ILog log = LogManager.GetLogger(typeof(Bar));
log.Info("message");

The benefits of second approach is that you can enable or disable some messages on the fly. But the problem is that I'm developing in EPiServer CMS and it has its own logging system that uses log4net and if I enable info logging at root level, then a lot of system logs will be written.

How do you use log4net? Each part of a system writes in its own logger, or everything is written in default logger, and configuration decides what to do next?

解决方案

Regarding how you log messages within code, I would opt for the second approach:

ILog log = LogManager.GetLogger(typeof(Bar));
log.Info("message");

Where messages sent to the log above will be 'named' using the fully-qualifed type Bar, e.g.

MyNamespace.Foo.Bar [INFO] message

The advantage of this approach is that it is the de-facto standard for organising logging, it also allows you to filter your log messages by namespace. For example, you can specify that you want to log INFO level message, but raise the logging level for Bar specifically to DEBUG:

<log4net>
    <!-- appenders go here -->
    <root>
        <level value="INFO" />
        <appender-ref ref="myLogAppender" />
    </root>

    <logger name="MyNamespace.Foo.Bar">
        <level value="DEBUG" />
    </logger>
</log4net>

The ability to filter your logging via name is a powerful feature of log4net, if you simply log all your messages to "myLog", you loose much of this power!

Regarding the EPiServer CMS, you should be able to use the above approach to specify a different logging level for the CMS and your own code.

For further reading, here is a codeproject article I wrote on logging:

这篇关于使用log4net的正确方式(记录器命名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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