0

I use the newest Castle Windsor container and I would like to create a logger, which will write to file what I want but also write the caller assembly name. So here is an example:

namespace Core.Datalayer
{
    public class Foo
    {
       public Foo(ILogger logger)
       {
          logger.Write("test line");
       }
    }
}

The container gives the instance correctly, but I cannot figure out how can I determine the Caller assembly name or the Caller class name? I would like that the logger writing to file something like this:

"assembly Core.Datalayer- test line"

Is there any possibility to determine that where instantiated the ILogger interface?

I tried the Assembly.GetCallingAssembly() but is always Castle.Core because the stack is full with Castle.Core calls.

Vajda Endre
  • 1,512
  • 1
  • 16
  • 26

1 Answers1

2

You can use a subdependency resolver. I think something like the code below should work for you.

public class Logger
{
    public Logger(string name)
    {
    }
}

public class LogResolver : ISubDependencyResolver
{
    public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
                           DependencyModel dependency)
    {
        return dependency.TargetType == typeof(Logger);
    }

    public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
                          DependencyModel dependency)
    {
        return new Logger(model.Name);
    }
}

install the logger int the container with

container.Kernel.Resolver.AddSubResolver(new LogResolver());
Marwijn
  • 1,681
  • 15
  • 24