为什么在Log4Net中命名记录器? [英] Why have named logger in Log4Net?

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

问题描述

我一直在研究使用Castle Windsor将Log4Net ILog注入类的方法.在大多数示例中,我看到温莎城堡在哪里可以提供促进者",该促进者"提供属性注入,并注入ILogger(而不是ILogger).我仅找到一个使用构造函数注入而未使用辅助方法的示例(请参阅

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的依赖关系而不使用反射或辅助方法(可将辅助方法与ctor注入一起使用???)带来了挑战.当看Ilya Kogan的问题时(上面的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.

专门处理ctor注入时,似乎有5种可能的选择...

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

  • 使用单例而不使用命名记录器(因此不在日志文件中报告)
  • 在ctor注入中使用反射(如Ilya Kogan的示例所示)
  • 使用属性注入(通过主持人)
  • 使用锐化后的AOP IL注入进行记录
  • 使用CTOR注射(通过促进剂)

推荐答案

使用温莎城堡(Castle Windsor)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天全站免登陆