log4net的记录器 - 为什么命名记录器? [英] Log4Net Logger - Why have named logger?

查看:147
本文介绍了log4net的记录器 - 为什么命名记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何注入log4net的ILog的使用到温莎城堡的类。在大多数例子中,我看到温莎城堡可以提供一个推动者,提供了财产注入,并注入了ILogger(非的ILog)。我已经发现当使用构造器注射只是一个例子,并且促进方法不使用(见温莎城堡依赖注入:使用调用类型作为参数

I have been investigating ways to inject the Log4Net ILog into classes using Castle Windsor. In most examples, I see where Castle Windsor can provide a "facilitator" that provides property injection, and injects the ILogger (not ILog). I have found only one example where contructor injection is used, and the facilitator method is not used (see Castle Windsor dependency injection: Use the caller type as a parameter)

在所有这些例子中,似乎log4net的希望有一个名为记录。大多数例子指的是log4net的静态方法LogManager.GetLogger(类的名字在这里)。这使得它定义CastleWindsor的依赖关系,而无需使用反射,或促进方法的一个挑战(可以协调器方法,构造函数注射用???)。当伊利亚高根(URL上方...)的问题来看,我想我不明白为什么我需要的,甚至想要一个名为记录。不能由我到处相同的名称使用记录?

In all these examples, it seems Log4Net wants to have a named logger. Most examples refer to the Log4Net static method LogManager.GetLogger(class name here). This makes it a challenge to define the dependancies for CastleWindsor without using reflection, or the facilitator method (can facilitator method be used with ctor injection???). When looking at the question by Ilya Kogan (URL above...), I guess I don't understand why I need, or even want a named logger. Can't I use a logger by the same name everywhere?

例如,我不能只是注册XXX的硬编码名称的记录? (这似乎做工精细,并在年底,我只是想记录 - 我不在乎这些记录器记录的吧...)有一个范围的问题吗?是否有内存泄漏问题?为什么不能/不应该记录器是一个单身?

For example, can't I just register the logger with hardcoded name of XXX? (It seems to work fine, and in the end, I just want to log - I don't care which logger logged it...) Is there a scope issue? Is there a memory leak issue? Why can't/shouldn't the logger be a singleton?

public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component.For<log4net.ILog>().UsingFactoryMethod(() => log4net.LogManager.GetLogger("xxx"))
                );
        }



更新:

当一些研究,一个硬编码命名的记录仪可以用来 - 比如XXX在我上面的例子,但如果记录器的配置记录器名称输出到日志文件,如果记录器名称是动态分配以同名的方法或类,你自动地得到参考日志源自何处。日志文件中的环境可以是非常有益的。

Upon some research, a hardcoded named logger can be used - such as XXX in my example above, but if the configuration of the logger outputs the logger name to the logfile and if the logger name is dynamically assigned to the same name as the method or class, you automagically get reference to where the logging originated from. Context within the log file can be very helpful.

在专门针对构造函数注入,似乎有5个可能的选择...

When specifically addressing ctor injection, there seems to be 5 possible options...


  • 使用独立的,并没有使用命名记录器(从而在日志文件中未报道)

  • 与男星注射液(如被看见在伊利亚高根的例子)
  • $ b $使用反射b
  • 使用属性注入(通过主持人)

  • 使用锋利后AOP IL注射记录

  • 使用CTOR注射(通过主持人)

  • Use a singleton and not use named loggers (thus not reported in log file)
  • Use reflection with ctor injection (as seen in Ilya Kogan's example)
  • Use property injection (via facilitators)
  • Use post-sharp AOP IL Injection for logging
  • Use CTOR injection (via facilitators)

推荐答案

使用温莎城堡到DI的log4net的日志推荐的方法是使用设施。一个已经为log4net的创建,并且其使用证明在本教程

The recommended way to DI an Log4Net Log using Castle Windsor is to use Facilities. One has already been created for Log4Net and its use is demonstrated in this tutorial.

这篇关于log4net的记录器 - 为什么命名记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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