Castle Windsor依赖注入:使用调用者类型作为参数
以下类在 Windsor Castle 容器中注册:
namespace MyNamespace
{
class Foo : IFoo
{
public Foo(ILog log)
{
_log = log;
}
...
}
}
ILog
是一个 log4net 记录器,当前注册如下:
Component.For<ILog>().UseFactoryMethod(
() => LogManager.GetLogger(Assembly.GetCallingAssembly().FullName));
我想创建一个名为 调用类型 的记录器(在这种情况 "MyNamespace.Foo"
) 而不是像调用程序集。除了使用反射来处理堆栈帧之外,还有其他方法可以做到这一点吗?
The following class is registered in the Windsor Castle container:
namespace MyNamespace
{
class Foo : IFoo
{
public Foo(ILog log)
{
_log = log;
}
...
}
}
ILog
is a log4net logger, currently registered like this:
Component.For<ILog>().UseFactoryMethod(
() => LogManager.GetLogger(Assembly.GetCallingAssembly().FullName));
I would like to create a logger named like the calling type (in this case "MyNamespace.Foo"
) rather than like the calling assembly. Is there a way to do it, except for using reflection to play with stack frames?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
温莎城堡伐木设施可以满足您的需求。它已经与 log4net 或 NLog 集成(请参阅:如何设置日志记录设施)。
如果您仍然好奇,可以检查源代码以了解它到底是如何完成的。查看 log4net 集成源 和日志记录工具源和特别是在 LoggerResolver 类(负责名称解析)。
The castle windsor logging facility does what you want. It has integration with log4net or NLog already (see: how to set up logging facility).
If you are curious still, you can examine the source code to see how exactly it can be done. Take a look at the log4net Integration source and the logging facility source and especially at the LoggerResolver class (which does the name resolving).