问题
I have a thin wrapper around log4net
and I am trying to use the type of the calling class to get a logger from log4net.LogManager
by using Castle.Windsor
.
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());
}
// ...
}
I have tried this registration in Castle.Windsor
(2.5.1) and I am able to get the correct logger for Foo
. Am I mis-using dynamic parameters for this scenario? Is there a more elegant solution?
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);
}
};
})
);
Edit: I've updated the solution using 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;
}
}
Registration:
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));
回答1:
You do not need to register CommonLog. Your sub dependency resolver can new one up for you.
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 Example of a logger provider
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 );
}
}
来源:https://stackoverflow.com/questions/9509115/dependency-on-log4net-logger-and-retrieve-logger-by-caller-type-using-castle-win