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

1

3

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?

Exchequer answered 21/4, 2011 at 5:52 Comment(2)
have you looked at the logging facility in castle?Inedited
See https://mcmap.net/q/1951932/-why-have-named-logger-in-log4net for possible answer to thisJemmy
I
3

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

Inedited answered 21/4, 2011 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.