Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 使用DI在NLog目标中设置属性_Dependency Injection_Inversion Of Control_Nlog - Fatal编程技术网

Dependency injection 使用DI在NLog目标中设置属性

Dependency injection 使用DI在NLog目标中设置属性,dependency-injection,inversion-of-control,nlog,Dependency Injection,Inversion Of Control,Nlog,我正在使用NLog,并且有一个自定义目标。我想向这个目标注入一个依赖项。依赖项已在Unity容器中注册。我在一个MVC应用程序中完成了这个任务,DI容器正在应用程序启动中设置 这里有一个类似问题的答案,但我不确定如何将其应用到我的代码中重要的部分是设置此自定义处理程序,这将为nlog使用的类型调用。 确保在完成此操作并注册依赖项后初始化nlog register your instances ..... ConfigurationItemFactory.Default.CreateInstanc

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


这里有一个类似问题的答案,但我不确定如何将其应用到我的代码中

重要的部分是设置此自定义处理程序,这将为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(...)

重要的部分是设置这个自定义处理程序,这将被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(...)