Skip to content
Contextual logger injection for Autofac using Serilog
Branch: dev
Clone or download

Contextual logger injection for Autofac Build status NuGet Pre Release

When using Serilog, contextual loggers attach the logging type's name to log events so they can later be found and filtered:

var log = Log.ForContext<SomeClass>();
log.Information("This event is tagged with 'SomeClass'");

Applications that use IoC often accept dependencies as constructor parameters:

public class SomeClass
  readonly ILogger _log;
  public SomeClass(ILogger log)
    _log = log;
  public void Show()
    _log.Information("This is also an event from 'SomeClass'");

This library configures Autofac to automatically configure the correct contextual logger for each class into which an ILogger is injected.


First install from NuGet:

Install-Package AutofacSerilogIntegration

Next, create the root logger:

Log.Logger = new LoggerConfiguration()

Then when configuring the Autofac container, call RegisterLogger():

var builder = new ContainerBuilder();


If no logger is explicitly passed to this function, the default Log.Logger will be used.

You can’t perform that action at this time.