Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Latest commit

 

History

History

Serilog.Samples.Autofac

Logging with Autofac

Key aspects

  • ASP.NET Core Web App
  • Uses Autofac.Extensions.DependencyInjection for dependency injection.
  • Uses Serilog.AspNetCore to get information about ASP.NET's internal operations written to the same Serilog sinks as your application events.

Setup

  1. Create ASP.NET Core Web App:

    dotnet new webapp --name Serilog.Samples.Autofac
  2. Add Serilog packages:

    dotnet add package Serilog.AspNetCore
    dotnet add package Autofac.Extensions.DependencyInjection
  3. Configure web host to UseServiceProviderFactory (reference):

    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
  4. Configure web host to UseSerilog (reference):

    .UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
        .Enrich.FromLogContext()
        .WriteTo.Console());
  5. Call ConfigureContainer to configure Autofac (reference):

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Register your own things directly with Autofac here. Don't
        // call builder.Populate(), that happens in AutofacServiceProviderFactory
        // for you.
        builder.RegisterType<GreetingsService>().As<IGreetingsService>();
    }
  6. Create some log output messages (reference).

  7. Run the application:

    dotnet run