NLOG:依赖注入的自定义目标 [英] NLog: Dependency Injection for custom Targets

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

问题描述

我NLOG的用户我创造我自己的自定义目标。这一目标将使用一些仓库(使用NHibernate)坚持日志条目。

I am a user of NLog and I am creating my own custom target. This target will use some repositories (using NHibernate) to persist log entries.

是否有可能注入的使用任何IoC框架自定义目标所需的构造函数依赖,preferably StructureMap?

Is it possible to inject the required constructor dependencies of custom targets using any IoC framework, preferably StructureMap?

问候,

Ĵ

推荐答案

工具包更新到挂接,用自己的DI容器框架的作者。下面是一个可能的用法:

The author of the toolkit updated the framework to expose hooks for using your own DI container. The following is one possible usage:

   public class LoggingConfiguration : ILoggingConfiguration
{
    public void SetDependencyResolver(IContainer container)
    {
        ConfigurationItemFactory.Default.CreateInstance = (Type type) => container.GetInstance(type);
    }
}

public static class DiagnosticsConfiguration
{
    public static void Configure(Action<ILoggingConfiguration> configuration)
    {
        var config = new LoggingConfiguration();
        configuration(config);
    }
}

public interface ILoggingConfiguration
{
    void SetDependencyResolver(IContainer container);
}

public interface IContainer
{
    object GetInstance(Type type);
}

public class StructureMapDependencyFactory : IContainer
{
    public object GetInstance(Type type)
    {
        return ObjectFactory.GetInstance(type);
    }

    public T GetInstance<T>()
    {
        return ObjectFactory.GetInstance<T>();
    }
}

希望这将有助于走出一个人。

Hopefully this will help out someone.

Ĵ

这篇关于NLOG:依赖注入的自定义目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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