日志ninject解析的依赖应用程序启动 [英] Log ninject resolved dependencies application start-up

查看:112
本文介绍了日志ninject解析的依赖应用程序启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经开始使用Ninject 2版作为我们的Io​​C连同扩展名被命名约定解决容器。我们还利用log4net的为我们的记录。

我想是Ninject记录所有已发现的依赖性,什么将他们下定决心,preferably上的应用程序启动。

我已经找到了记录延长,但无法找到如何使用它来获得本文档或例子。

编辑:

由于这是在这里要求的是,登录时启动缺省绑定,使用类log4net的

公共类DefaultBindingGeneratorWithLogging:IBindingGenerator     {         私人只读的iKernel内核;

  ///<总结>
    ///初始化℃的新的实例;参见CREF =DefaultBindingGeneratorWithLogging/>类。
    ///< /总结>
    ///< PARAM NAME =仁与GT;内核< /参数>
    公共DefaultBindingGeneratorWithLogging(的iKernel内核)
    {
        this.kernel =内核;
    }

    ///<总结>
    ///创建绑定类型。
    ///< /总结>
    ///< PARAM NAME =输入>作为其中创建绑定的类型< /参数>
    ///< PARAM NAME =bindingRoot>用于创建绑定绑定根< /参数>
    ///<返回>
    ///的语法为创建绑定配置更多的选择。
    ///< /回报>
    公开的IEnumerable< IBindingWhenInNamedWithOrOnSyntax<对象>> CreateBindings(类型类型,IBindingRoot bindingRoot)
    {
        如果(type.IsInterface || type.IsAbstract)
        {
            产生中断;
        }

        键入interfaceForType = type.GetInterface(我+ type.Name,假);
        如果(interfaceForType == NULL)
        {
            产生中断;
        }

        VAR数= kernel.Get< ILog的>();
        如果(!(kernel.GetBindings(interfaceForType)。任何()))
        {
            bindingRoot.Bind(interfaceForType)。为了(类型).InTransientScope();
            如果(log.IsInfoEnabled&安培;&安培;!String.IsNullOrWhiteSpace(interfaceForType.FullName))
            {
                log.InfoFormat(映射{0}  - > {1},type.FullName,interfaceForType.FullName);
            }
        }
        其他
        {
            log.InfoFormat(没有地图{0}  - > {1}的映射已经存在,type.FullName,interfaceForType.FullName);
        }
    }
}
 

解决方案

您大概可以达到你正在尝试做的通过创建自己的Activati​​onStrategy的实例是什么。下面是我用监​​视激活/停用之一:

 公共类MyMonitorActivati​​onStrategy:Activati​​onStrategy
{
    私人ILogger _logger;

    公众覆盖无效激活(Ninject.Activati​​on.IContext背景下,Ninject.Activati​​on.InstanceReference参考)
    {
        如果(reference.Instance是ILogger)
        {
            _logger =(ILogger)reference.Instance;
        }
        _logger.Debug(Ninject激活:+ reference.Instance.GetType());
        base.Activate(背景下,参考);
    }

    公众覆盖无效停用(Ninject.Activati​​on.IContext背景下,Ninject.Activati​​on.InstanceReference参考)
    {
        _logger.Debug(Ninject停用:+ reference.Instance.GetType());
        base.Deactivate(背景下,参考);
    }
}
 

您连线该当你创建你的内核。

 保护覆盖的iKernel CreateKernel()
    {
    VAR内核=新StandardKernel();
        kernel.Components.Add&所述; IActivati​​onStrategy,MyMonitorActivati​​onStrategy>();

        kernel.Load<的AppModule>();

        返回内核;
    }
 

希望这有助于。

鲍勃·

We have started using Ninject version 2 as our IoC container along with the extension for resolving by naming conventions. We are also using log4net for our logging.

What I would like is for Ninject to log all the dependencies it has found and what will resolve them to, preferably on the application start-up.

I have found the logging extension, but can't find documentation or examples on how to use it to get this.

Edit:

Since it was requested here is the class that logs the default bindings on startup, using log4net

public class DefaultBindingGeneratorWithLogging : IBindingGenerator { private readonly IKernel kernel;

    /// <summary>
    /// Initializes a new instance of the <see cref="DefaultBindingGeneratorWithLogging"/> class.
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    public DefaultBindingGeneratorWithLogging(IKernel kernel)
    {
        this.kernel = kernel;
    }

    /// <summary>
    /// Creates the bindings for a type.
    /// </summary>
    /// <param name="type">The type for which the bindings are created.</param>
    /// <param name="bindingRoot">The binding root that is used to create the bindings.</param>
    /// <returns>
    /// The syntaxes for the created bindings to configure more options.
    /// </returns>
    public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
    {
        if (type.IsInterface || type.IsAbstract)
        {
            yield break;
        }

        Type interfaceForType = type.GetInterface("I" + type.Name, false);
        if (interfaceForType == null)
        {
            yield break;
        }

        var log = kernel.Get<ILog>();
        if (!(kernel.GetBindings(interfaceForType).Any()))
        {
            bindingRoot.Bind(interfaceForType).To(type).InTransientScope();
            if (log.IsInfoEnabled && !String.IsNullOrWhiteSpace(interfaceForType.FullName))
            {
                log.InfoFormat("Mapping {0} -> {1}", type.FullName, interfaceForType.FullName);
            }
        }
        else
        {                
            log.InfoFormat("Didn't map {0} -> {1} mapping already exists", type.FullName, interfaceForType.FullName);
        }
    }
}

解决方案

You can probably achieve what you are trying to do by creating your own instance of 'ActivationStrategy'. Here is one that I was using to monitor activation / deactivation:

public class MyMonitorActivationStrategy : ActivationStrategy
{
    private ILogger _logger;

    public override void Activate(Ninject.Activation.IContext context, Ninject.Activation.InstanceReference reference)
    {
        if(reference.Instance is ILogger)
        {
            _logger = (ILogger)reference.Instance;
        }
        _logger.Debug("Ninject Activate: " + reference.Instance.GetType());
        base.Activate(context, reference);
    }

    public override void Deactivate(Ninject.Activation.IContext context, Ninject.Activation.InstanceReference reference)
    {
        _logger.Debug("Ninject DeActivate: " + reference.Instance.GetType());
        base.Deactivate(context, reference);
    }
}

You wire this when you create your kernel.

    protected override IKernel CreateKernel()
    {
    var kernel = new StandardKernel();
        kernel.Components.Add<IActivationStrategy, MyMonitorActivationStrategy>();

        kernel.Load<AppModule>();

        return kernel;
    }

Hope this helps.

Bob

这篇关于日志ninject解析的依赖应用程序启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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