/Autofac.Extras.NLog

An Autofac module to integrate Autofac and NLog, it supports both constructor and property injection.

Primary LanguageC#MIT LicenseMIT

Autofac Loggging Module for NLog

Build status

Nuget package available, to install run the following command;

Install-Package Autofac.Extras.NLog

Register NLogModule to Autofac

It attaches to Component Registration and creates logger for requested type.

containerBuilder.RegisterModule<NLogModule>();

Register SimpleNLogModule to Autofac

It is useful when ILogger resolved from Service Locator.

containerBuilder.RegisterModule<SimpleNLogModule>();

NLogModule and SimpleNLogModule supports both constructor and property injection.

  • Constructor sample
public class SampleClassWithConstructorDependency : ISampleInterface
{
    private readonly ILogger _logger;

    public SampleClassWithConstructorDependency(ILogger logger)
    {
      _logger = logger;
  }        
}
  • Property Sample
public class SampleClassWithPropertyDependency : ISampleInterface
{
   public ILogger Logger { get; set; }
}
  • Service Locator Sample
 public class SampleClassToResolveLoggerFromServiceLocator : ISampleClass
    {
        private readonly ILogger _logger;
        
        public SampleClassToResolveLoggerFromServiceLocator(ILifetimeScope serviceLocator)
        {
            _logger = serviceLocator.Resolve<ILogger>();
        }
    }