Ninject注入ILog依赖项 [英] Ninject to inject ILog dependency

查看:96
本文介绍了Ninject注入ILog依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该论坛上浏览了其他一些与Ninject和Log4net相关的帖子,但是似乎都没有一个问题可以解决(或解决).

I have gone through some other posts in this forum that are related to Ninject and Log4net, but none seemed to address the issue (or resolve it).

代码看起来像

IKernel kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = true }); 
kernel.Load(Assembly.GetExecutingAssembly());
Program pgm = new Program(kernel.Get<IFSLog>());

在上面的最后一行中引发异常,并显示消息激活ILog出错.没有匹配的绑定可用..."

Exception is thrown in the last line above with message "Error activating ILog. No matching bindings are available... "

IFSLog是我的程序集中定义的接口,其实现对log4Net Ilog对象的依赖关系如下所示

IFSLog is an interface defined in my assembly and its implementation has a dependency on the log4Net Ilog object as below

public class Log4NetLog : IFSLog {
  private ILog logger;
  public Log4NetLog(ILog log) {
    this.logger = log;
  }
  ...
}

该项目引用了Ninject.extensions.logging.log4net程序集,因此我的理解是应该从此处标识ILog绑定.

The project references the Ninject.extensions.logging.log4net assembly, so my understanding is that the ILog binding should be identified from there.

还尝试了手动指定绑定的另一种方式

Also tried the alternate way of specifying bindings manually

public class Bindings : NinjectModule {
    public override void Load() {
        Bind<IFSLog>().To<Log4NetLog>();
    }
}

并将内核初始化为

IKernel kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false }, 
  new INinjectModule[] {new Bindings(), new Log4NetModule()});

还是一样的结果.任何帮助,不胜感激,在此先感谢...

Still same result. Any help much appreciated, thanks in advance...

推荐答案

事实证明,Ninject.Extensions.Logging.Log4Net和Ninject.Extensions.Logging.NLog提供的是Log4net和Nlog接口的抽象,因此您可以轻松地将NLog与Log4Net交换.

As it turns out, what Ninject.Extensions.Logging.Log4Net and Ninject.Extensions.Logging.NLog offer is an abstraction of the Log4net and Nlog interfaces, so you can easily swap NLog with Log4Net.

两个扩展都只为Ninject.Extensions.Logging.ILoggerFactoryNinject.Extensions.Logging.ILogger创建绑定.这就是为什么ILog没有绑定的原因.

Both extensions only create a binding for Ninject.Extensions.Logging.ILoggerFactory and Ninject.Extensions.Logging.ILogger. So that's why there is no binding for ILog.

如果将ILogger而不是ILog注入到Log4NetLog : IFSLog中,它将起作用.

If you inject ILogger instead of ILog into your Log4NetLog : IFSLog it will work.

或者您可以跳过使用ninject扩展名并将其自己连接起来,然后直接使用log4net ILog界面(您将IFSLog用于什么目的?).

Or you can skip using the ninject extensions and wire it up yourself and use the log4net ILog interface directly (what are you using the IFSLog for?).

我们曾经直接使用log4net ILog并简化了这样的事情:

We used to use log4net ILog directly and simplified things like this:

internal class LogProvider : Provider<ILog>
{
    protected override ILog CreateInstance(IContext context)
    {
        Type typeLoggerIsInjectedInto = context.Request.ParentContext.Plan.Type;
        return LogManager.GetLogger(typeLoggerIsInjectedInto);
    }
}

IKernel.Bind<ILog>().ToProvider<LogProvider>();

这篇关于Ninject注入ILog依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆