Castle Windsor依赖项注入:将调用者类型用作参数 [英] Castle Windsor dependency injection: Use the caller type as a parameter

查看:81
本文介绍了Castle Windsor依赖项注入:将调用者类型用作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在温莎城堡的容器中注册了以下课程:

The following class is registered in the Windsor Castle container:

namespace MyNamespace
{
    class Foo : IFoo
    {
        public Foo(ILog log)
        {
             _log = log;
        }
        ...
    }
}

ILog是一个log4net记录器,当前注册如下:

ILog is a log4net logger, currently registered like this:

Component.For<ILog>().UseFactoryMethod(
    () => LogManager.GetLogger(Assembly.GetCallingAssembly().FullName));

我想创建一个名为调用类型(在本例中为"MyNamespace.Foo")而不是调用程序集的记录器.除了使用反射来处理堆栈帧以外,还有其他方法吗?

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?

推荐答案

温莎城堡测井设施可以满足您的需求.它已经与log4net或NLog集成(请参阅:如何设置日志记录设施).

The castle windsor logging facility does what you want. It has integration with log4net or NLog already (see: how to set up logging facility).

如果您仍然感到好奇,可以检查源代码以了解如何精确地完成它.看看 log4net集成源记录工具的来源和尤其是在LoggerResolver类(用于解析名称)上.

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).

这篇关于Castle Windsor依赖项注入:将调用者类型用作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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