使用log4net的正确方法 [英] Correct way of using log4net

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

问题描述

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

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");

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

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.

如何使用log4net的?系统的每一部分写在了自己的记录,或者一切都写在默认记录仪,并配置决定下一步该怎么做?

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?

推荐答案

至于你怎么日志中code消息,我会选择第二种方法:

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

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

在哪里发送到日志将上面命名为使用完全组队参加类型的消息酒吧,例如:

MyNamespace.Foo.Bar [INFO] message

这种方法的优势在于它是事实上的标准组织记录,它也可以让你通过命名空间来过滤日志消息。例如,您可以指定要记录INFO级别的消息,但提高的日志记录级别酒吧专为DEBUG:

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

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

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

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!

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

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.

有关进一步的阅读,这里是一个$ C $的CProject文章我写的日志:

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

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

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