Autofac.log4net is a library that allows easy integration of log4net with the Autofac IoC container. It contains a Log4NetModule to support injection of ILog properties and constructor parameter to instances created through the Autofac container.
- Supports .NET 4.6.1 and above.
- The package has 2 package dependencies:
- log4net >= 2.0.8
- Autofac >= 6.0.0
- If a class has a property or constructor parameter of type ILog, it will inject it with a logger.
- The logger instance will be by default the logger with the class type name.
- If the type/namespace of the class is mapped in the module, the logger instance will be the mapped logger.
- There are 2 type of mappings the Log4NetModule:
- Mapping by types.
- Mapping by namespaces - when injecting the logger, the most specific namespace mapping will be used.
- The module allows configuring the application to a custom logger configuration file and watching it.
public class InjectableClass {
private readonly ILog _logger;
public InjectableClass(ILog logger){
_logger = logger;
}
}
public class InjectableClass {
public ILog Logger { get; set; }
}
var builder = new ContainerBuilder();
builder.RegisterModule<Log4NetModule>();
var builder = new ContainerBuilder();
var loggingModule = new Log4NetModule("logger.config", true);
builder.RegisterModule(loggingModule);
var builder = new ContainerBuilder();
var loggingModule = new Log4NetModule();
loggingModule.MapTypeToLoggerName(typeof(InjectableClass), "Logger1");
loggingModule.MapNamespaceToLoggerName("Autofac.log4net", "Logger2");
builder.RegisterModule(loggingModule);