依赖Log4Net Logger和使用Castle Windsor按调用方类型检索Logger

本文关键字:Logger 方类型 检索 调用 类型 Windsor Log4Net 依赖 Castle | 更新日期: 2023-09-27 18:26:18

我有一个关于log4net的瘦包装,我正试图使用调用类的类型通过使用Castle.Windsorlog4net.LogManager获取记录器。

public class Foo: IFoo
{
    private readonly ICommonLog _logger;
    public Foo(ICommonLog logger)
    {
        _logger = logger;
    }
    public void FooBar()
    {
        _logger.Info("Enter FooBar Method");
    }
}
public class CommonLog : ICommonLog
{
    private readonly log4net.ILog _logger;
    public CommonLog(Type loggerType)
    {
        _logger = log4net.LogManager.GetLogger(loggerType); 
    }
    public void Info(string message)
    {         
        _logger.Info(message.Cleanse());
    }
    // ...
}

我已经在Castle.Windsor(2.5.1)中尝试了此注册,并且我能够获得Foo的正确记录器。在这种情况下,我是否错误地使用了动态参数?有没有更优雅的解决方案?

container.Register(Component.For<ICommonLog>().ImplementedBy(typeof(CommonLog)).
   LifeStyle.Transient);
container.Register(Component.For<IFoo>().ImplementedBy(typeof(Foo)).
   LifeStyle.Transient.DynamicParameters(
       (k, d) =>
       {
          var component = k.Resolve<ICommonLog>(new {loggerType = typeof (Foo)});
          d["logger"] = component;
          return r =>
          {
              if (component != null)
              {
                  r.ReleaseComponent(component);
              }
          };
       })
  );

编辑:我已经使用ISubDependencyResolver:更新了解决方案

public class LoggerResolver : ISubDependencyResolver
{
    private readonly IKernel kernel;
    public LoggerResolver(IKernel kernel)
    {
        this.kernel = kernel;
    }
    public bool CanResolve(CreationContext context,
                           ISubDependencyResolver contextHandlerResolver,
                           ComponentModel model,
                           DependencyModel dependency)
    {
        return dependency.TargetType == typeof (ICommonLog);
    }
    public object Resolve(CreationContext context,
                          ISubDependencyResolver contextHandlerResolver,
                          ComponentModel model,
                          DependencyModel dependency)
    {
        if (CanResolve(context, contextHandlerResolver, model, dependency))
        {
            return kernel.Resolve<ICommonLog>(new {loggerType = model.Implementation});
        }
        return null;
    }
}

注册:

container.Register(Component.For<ICommonLog>().ImplementedBy(typeof(CommonLog))
    .LifeStyle.Transient);
container.Register(Component.For<IFoo().ImplementedBy(typeof(Foo))
    .LifeStyle.Transient);
container.Kernel.Resolver.AddSubResolver(new LoggerResolver(container.Kernel));

依赖Log4Net Logger和使用Castle Windsor按调用方类型检索Logger

您不需要注册CommonLog。您的子依赖解析程序可以为您新建一个。

container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Transient);
container.Kernel.Resolver.AddSubResolver(new LoggerResolver(container.Kernel));
public class LoggerResolver : ISubDependencyResolver
{
    ...
    public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
    {
        //No need to can resolve, Windsor will do this for you
        return new CommonLog(model.Implementation);
    }
}

EDIT记录器提供商示例

container.Register(Component.For<ILoggerProvider>().ImplementedBy<Log4NetLoggerProvider >().LifeStyle.Transient);
public interface ICommonLoggerProvider
{
    ICommonLog GetLogger( Type type );
}
public class Log4NetLoggerProvider : ICommonLoggerProvider
{
    public ICommonLog GetLogger(Type type)
    {
        return new Log4NetLogger(type);
    }
}
public class LoggerResolver : ISubDependencyResolver
{
    ...
    public object Resolve( CreationContext context, ISubDependencyResolver contextHandlerResolver, Castle.Core.ComponentModel model, DependencyModel dependency )
    {
        return kernel.Resolve<ICommonLoggerProvider>().GetLogger( model.Implementation );
    }
}