我有一個.NET MVC4項目,我想用我的記錄器包裝NLOG(我有理由),並將其注入Castle Windsor。我嘗試下面的東西。我沒有機會成功地完成它。溫莎城堡包裝NLOG
public interface ICommonLogger
{
void Info(string message);
}
public class CommonLogger : ICommonLogger
{
private readonly Logger _logger;
public CommonLogger(Type loggerType)
{
_logger = LogManager.GetLogger(loggerType.FullName, loggerType);
}
public void Info(string message)
{
_logger.Info(message);
}
}
container.Register(Component.For<ICommonLogger>().ImplementedBy(typeof(CommonLogger)).LifeStyle.Transient);
container.Kernel.Resolver.AddSubResolver(new LoggerResolver(container.Kernel));
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(ICommonLogger);
}
public object Resolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
if (CanResolve(context, contextHandlerResolver, model, dependency))
{
return kernel.Resolve<ICommonLogger>(new { loggerType = model.Implementation });
}
return null;
}
}
但我混得圖像錯誤。
如果我直接用NLOG與
container.AddFacility<LoggingFacility>(f => f.UseNLog());
它與城堡ILogger實現,但我希望它爲自己的ICommonLogger implemantation工作。
我做錯了什麼?
非常感謝。 –