2

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

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

4

1 回答 1

0

重要的部分是设置这个自定义处理程序,这将为 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(...)
于 2016-07-29T17:41:53.777 回答