Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog: Dependency Injection for custom Targets

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

like image 538
JB. Avatar asked May 16 '11 14:05

JB.


People also ask

How to improve performance of nlog targets?

Custom NLog Targets that uses Target.RenderLogEvent () instead of Layout.Render () will experience a performance boost. Reason: NLog 4.4.2 introduced the ability to reuse the same array-buffer to reduce memory allocation along with better performance for FileTarget.

What's new in nlog-NuGet-package?

NLog Targets that has been extracted from the default NLog-nuget-package has now also been updated to make use of the new Layout<T> -features. NLog ScopeContext has been introduced to cater for Microsoft Extension Logging (MEL) ILogger.BeginScope .

What's new in optimizebufferreuse for nlog?

The OptimizeBufferReuse performance optimization will now always be enabled for all NLog extensions targets. Custom NLog Targets that uses Target.RenderLogEvent () instead of Layout.Render () will experience a performance boost.

How to update the nlog-NuGet-config?

NLog.LogManager.Setup().SetupExtensions(ext => ext.AutoLoadExtensions()); Alternative one can update the NLog.config with the option AutoLoadExtensions="true": The following NLog targets has been extracted from the NLog-nuget-package to their own isolated nuget-packages:


2 Answers

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!

like image 80
kamranicus Avatar answered Sep 21 '22 05:09

kamranicus


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

like image 29
JB. Avatar answered Sep 22 '22 05:09

JB.