【问题标题】:Using DI to set a property in an NLog Target使用 DI 在 NLog 目标中设置属性
【发布时间】:2012-05-28 02:35:24
【问题描述】:

我正在使用 NLog 并且有一个自定义目标。我想在这个目标中注入一个依赖项。依赖项已在 Unity 容器中注册。我是在 MVC 应用程序中,DI 容器正在 Application_Start 中设置

https://stackoverflow.com/a/9704442 这里有一个类似问题的答案,但我不确定如何将其应用于我的代码

【问题讨论】:

    标签: dependency-injection inversion-of-control nlog


    【解决方案1】:

    重要的部分是设置这个自定义处理程序,这将为 nlog 使用的类型调用。 确保在完成此操作并注册依赖项后初始化 nlog

    register your instances
    .....
    ConfigurationItemFactory.Default.CreateInstance = (Type type) =>
    {
        object instance;
        if(unity/ninject/etc.TryResolve(type, out instance))
          return instance;
        else
          return Activator.CreateInstance(type); //this is to support the other types that come with NLog
    };
    ......
     ConfigureNLog(...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多