开发者

Castle Windsor dependency injection: Use the caller type as a parameter

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(
    () =>开发者_如何学Python 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜