Skip to content

Autofac module for using the log4net logging framework

License

Notifications You must be signed in to change notification settings

eran-gil/autofac.log4net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Autofac.log4net

Description

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.

CI Status

Requirements

  • Supports .NET 4.6.1 and above.
  • The package has 2 package dependencies:
    • log4net >= 2.0.8
    • Autofac >= 6.0.0

Features

  • 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:
    1. Mapping by types.
    2. 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.

License:

MIT License

Examples

Class with an ILog Constructor Parameter

public class InjectableClass {
    private readonly ILog _logger;
    
    public InjectableClass(ILog logger){
        _logger = logger;
    }
}

Class with an ILog Property

public class InjectableClass {
    public ILog Logger { get; set; }
}

Simple Module Registration

var builder = new ContainerBuilder();
builder.RegisterModule<Log4NetModule>();

Custom Module Registration

var builder = new ContainerBuilder();
var loggingModule = new Log4NetModule("logger.config", true);
builder.RegisterModule(loggingModule);

Mapping Types and Namespaces to Loggers

var builder = new ContainerBuilder();
var loggingModule = new Log4NetModule();
loggingModule.MapTypeToLoggerName(typeof(InjectableClass), "Logger1");
loggingModule.MapNamespaceToLoggerName("Autofac.log4net", "Logger2");
builder.RegisterModule(loggingModule);