log4net的正确使用方法(logger命名)
有两种配置和使用 log4net 的方法。第一个是当我可以配置自己的附加程序和关联的记录器时:
<!-- 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>
然后当我想在日志中写入某些内容时,我可以执行以下操作:
ILog log = LogManager.GetLogger("myLog");
log.Info("message");
程度:
<!-- language: xml -->
<root>
<level value="Error" />
<appender-ref ref="myLogAppender" />
</root>
另一种使用它的方法是将根配置为我想要的详细 在这种情况下,我可以记录这样的消息:
ILog log = LogManager.GetLogger(typeof(Bar));
log.Info("message");
第二种方法的好处是您可以即时启用或禁用某些消息。但问题是我正在 EPiServer CMS 中进行开发,它有自己的日志系统,使用 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");
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于如何在代码中记录消息,我会选择第二种方法:
发送到上面日志的消息将使用完全限定类型
Bar
进行“命名”,例如这种方法的优点是它是组织日志记录的事实上的标准,它还允许您按名称空间过滤日志消息。例如,您可以指定要记录 INFO 级别消息,但将
Bar
的日志记录级别专门提高到 DEBUG:通过名称过滤日志记录的能力是 log4net 的一个强大功能,如果您只需将所有消息记录到
“myLog”
,您就失去了大部分这种能力!对于 EPiServer CMS,您应该能够使用上述方法为 CMS 和您自己的代码指定不同的日志记录级别。
为了进一步阅读,这里是我写的关于日志记录的 codeproject 文章:
Regarding how you log messages within code, I would opt for the second approach:
Where messages sent to the log above will be 'named' using the fully-qualifed type
Bar
, e.g.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: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:
我的回答可能会晚一些,但我认为它可以帮助新手。除非进行如下更改,否则您不会看到执行的日志。
2 实施Log4net时必须更改文件。
在 [app.config] 内:
首先,在“configSections”下,您需要添加以下代码;
然后,在“配置”块下,您需要编写下面的代码段。(这段代码是根据我的需要定制的,但它的工作方式就像魅力。)
在调用类内部:
在您要使用此 log4net 的类中,您需要声明以下代码段。
现在,您可以在同一类中的任何位置准备好通话记录。以下是您在执行操作时可以调用的方法之一。
My Answer might be coming late, but I think it can help newbie. You shall not see logs executed unless the changes are made as below.
2 Files have to be changes when you implement Log4net.
Inside [app.config] :
First, under 'configSections', you need to add below piece of code;
Then, under 'configuration' block, you need to write below piece of code.(This piece of code is customised as per my need , but it works like charm.)
Inside Calling Class :
Inside the class where you are going to use this log4net, you need to declare below piece of code.
Now, you are ready call log wherever you want in that same class. Below is one of the method you can call while doing operations.
我没有命名我的调用类,而是开始使用以下内容:
这样,我可以在每个使用 log4net 的类中使用同一行代码,而不必在复制和粘贴时记住更改代码。
或者,我可以创建一个日志记录类,并让所有其他类继承我的日志记录类。
Instead of naming my invoking class, I started using the following:
In this way, I can use the same line of code in every class that uses log4net without having to remember to change code when I copy and paste.
Alternatively, i could create a logging class, and have every other class inherit from my logging class.
第二种方法的缺点是带有创建的记录器的大型存储库。如果定义了 root 并且未定义类记录器,则此记录器会执行相同的操作。生产系统的标准场景是使用少量专用于类组的记录器。对不起我的英语。
Disadvantage of second approach is big repository with created loggers. This loggers do the same if root is defined and class loggers are not defined. Standard scenario on production system is using few loggers dedicated to group of class. Sorry for my English.