Castle Windsor dependency injection: Use the calle

2019-07-03 17:44发布

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?

1条回答
疯言疯语
2楼-- · 2019-07-03 18:16

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

查看更多
登录 后发表回答