NLog: Dependency Injection for custom Targets
Asked Answered
M

2

5

I am a user of NLog and I am creating my own custom target. This target will use some repositories (using NHibernate) to persist log entries.

Is it possible to inject the required constructor dependencies of custom targets using any IoC framework, preferably StructureMap?

Regards,

J

Marabou answered 16/5, 2011 at 14:16 Comment(0)
M
2

The author of the toolkit updated the framework to expose hooks for using your own DI container. The following is one possible usage:

   public class LoggingConfiguration : ILoggingConfiguration
{
    public void SetDependencyResolver(IContainer container)
    {
        ConfigurationItemFactory.Default.CreateInstance = (Type type) => container.GetInstance(type);
    }
}

public static class DiagnosticsConfiguration
{
    public static void Configure(Action<ILoggingConfiguration> configuration)
    {
        var config = new LoggingConfiguration();
        configuration(config);
    }
}

public interface ILoggingConfiguration
{
    void SetDependencyResolver(IContainer container);
}

public interface IContainer
{
    object GetInstance(Type type);
}

public class StructureMapDependencyFactory : IContainer
{
    public object GetInstance(Type type)
    {
        return ObjectFactory.GetInstance(type);
    }

    public T GetInstance<T>()
    {
        return ObjectFactory.GetInstance<T>();
    }
}

Hopefully this will help out someone.

J

Marabou answered 14/3, 2012 at 14:59 Comment(1)
Just want to note for others, the key line here is ConfigurationItemFactory.Default.CreateInstance. For Ninject, this would simply be: ConfigurationItemFactory.Default.CreateInstance = (type) => Kernel.TryGet(type), but JC provides a better modular pattern.Smite
S
8

I want to provide some context for people, since I was confused at first by your answer 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;
    }
}

This shows how you set up the instance creation and how it all fits together with NLog. Hope that helps other people!

Smite answered 13/1, 2013 at 1:5 Comment(0)
M
2

The author of the toolkit updated the framework to expose hooks for using your own DI container. The following is one possible usage:

   public class LoggingConfiguration : ILoggingConfiguration
{
    public void SetDependencyResolver(IContainer container)
    {
        ConfigurationItemFactory.Default.CreateInstance = (Type type) => container.GetInstance(type);
    }
}

public static class DiagnosticsConfiguration
{
    public static void Configure(Action<ILoggingConfiguration> configuration)
    {
        var config = new LoggingConfiguration();
        configuration(config);
    }
}

public interface ILoggingConfiguration
{
    void SetDependencyResolver(IContainer container);
}

public interface IContainer
{
    object GetInstance(Type type);
}

public class StructureMapDependencyFactory : IContainer
{
    public object GetInstance(Type type)
    {
        return ObjectFactory.GetInstance(type);
    }

    public T GetInstance<T>()
    {
        return ObjectFactory.GetInstance<T>();
    }
}

Hopefully this will help out someone.

J

Marabou answered 14/3, 2012 at 14:59 Comment(1)
Just want to note for others, the key line here is ConfigurationItemFactory.Default.CreateInstance. For Ninject, this would simply be: ConfigurationItemFactory.Default.CreateInstance = (type) => Kernel.TryGet(type), but JC provides a better modular pattern.Smite

© 2022 - 2024 — McMap. All rights reserved.