.net - NLog:自定义目标的依赖注入(inject)

标签 .net dependency-injection ioc-container structuremap nlog

我是 NLog 的用户,我正在创建自己的自定义目标。这个目标将使用一些存储库(使用 NHibernate)来持久化日志条目。

是否可以使用任何 IoC 框架(最好是 StructureMap)注入(inject)自定义目标所需的构造函数依赖项?

问候,

Ĵ

最佳答案

我想为人们提供一些背景信息,因为我起初对你的回答 JC 感到困惑。

public Program {

    //
    // Static constructor
    //
  static Program() {
    // Set up Ninject
    var kernel = new StandardKernel();

    // Register bindings
    RegisterServices(kernel);

    // Set up Ninject logging config
    NLog.Config.ConfigurationItemFactory.Default.CreateInstance = 
        (type) => kernel.TryGet(type);

    // Continue on!
  }

  private static void RegisterServices(IKernel kernel) {
    // bind services!
    kernel.Bind<IMyClass>().To<MyClass>();
  }
}

[Target("Custom")]
public class CustomTarget : TargetWithLayout {

    private IMyClass _myClass;
    public CustomTarget(IMyClass myClass) {

        // This will be injected!
        _myClass = myClass;
    }
}

这显示了如何设置实例创建以及它如何与 NLog 结合在一起。希望对其他人有所帮助!

关于.net - NLog:自定义目标的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018721/

相关文章:

c# - .NET 6 API 终结点的取消行为在本地和 Azure 应用服务 Linux 托管之间有所不同

.net - LINQ to SQL : how to update the only field without retrieving whole entity

.net - 使用哪个? Visual Studio 开发服务器或本地 IIS Web 服务器?

c# - 改善互联网速度较低且使用远距离托管的 WCF Web 服务的 Windows 窗体客户端的用户体验

dependency-injection - Autofac DI,类型不可分配给服务

android - Roboguice 应用程序 @InjectView

c# - 使用动态注入(inject)类创建委托(delegate)

entity-framework - 在类库中注册容器(IoC)?

Spring DI - REST 服务中的 Autowired 属性为空

java - 如何使用当前的spring bean访问另一个spring bean中的数据