Nuget package available, to install run the following command;
Install-Package Autofac.Extras.NLog
It attaches to Component Registration and creates logger for requested type.
containerBuilder.RegisterModule<NLogModule>();
It is useful when ILogger resolved from Service Locator.
containerBuilder.RegisterModule<SimpleNLogModule>();
- 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>();
}
}