Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(
    () => 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?

like image 480
Ilya Kogan Avatar asked Feb 01 '26 02:02

Ilya Kogan


1 Answers

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

like image 141
Can Gencer Avatar answered Feb 02 '26 19:02

Can Gencer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!