2
votes

If you are using the LoggingFacility in Castle Windsor the container will automatically resolve the logger associated with your class if you have optional logger dependencies in your class (an ILogger property that castle can inject the logger into), but how can I leverage the facility if I want to implement the logging using AOP (interceptor approach)? I basically want to write something like:

    public void Intercept(IInvocation invocation)
    {
        ILogger logger = LogManager.GetLogger(invocation.TargetType);
        //..
    }

But there is no LogManager to speak of in the Castle framework. What is the best approach to solving this? Should I just ignore the facility approach and use log4net directly in the interceptor?

1

1 Answers

4
votes

Take a dependency on Castle.Core.Logging.ILoggerFactory in your constructor and create the logger from the factory in the Intercept method

public class LoggingInterceptor : IInterceptor
{
    readonly ILoggingFactory loggingFactory;

    public LoggingInterceptor(ILoggingFactory loggingFactory)
    {
        this.loggingFactory = loggingFactory;
    }

    public void Intercept(IInvocation invocation)
    {
        ILogger logger = loggingFactory.Create(invocation.TargetType);
        //..
    }
}