Dependency injection 使用DI在NLog目标中设置属性
我正在使用NLog,并且有一个自定义目标。我想向这个目标注入一个依赖项。依赖项已在Unity容器中注册。我在一个MVC应用程序中完成了这个任务,DI容器正在应用程序启动中设置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使用的类型调用。 确保在完成此操作并注册依赖项后初始化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(...)