Skip to content

A dependency module (derived from Autofac.Module) that enables injecting the same contextual logger as ILogger instead of ILogger<TCategoryName>

License

Notifications You must be signed in to change notification settings

gcsizmadia/EgonsoftHU.Extensions.Logging.Autofac

Repository files navigation

Egonsoft.HU Logging Extensions for Autofac

GitHub Nuget Nuget

A dependency module (derived from Autofac.Module) that enables injecting the same contextual logger as Microsoft.Extension.Logging.ILogger instead of Microsoft.Extension.Logging.ILogger<TCategoryName>.

Table of Contents

Introduction

The motivation behind this project is to simplify the constructor injection of Microsoft logger.

So instead of using the ILogger<TCategoryName> interface type:

using Microsoft.Extensions.Logging;

public class MyService
{
    private readonly ILogger<MyService> logger;

    public MyService(ILogger<MyService> logger)
    {
        this.logger = logger;
    }
}

We can simply use the ILogger interface type and still get the same contextual logger instance:

using Microsoft.Extensions.Logging;

public class MyService
{
    private readonly ILogger logger;

    public MyService(ILogger logger)
    {
        this.logger = logger;
    }
}

Releases

You can download the package from nuget.org.

Please note: Each package version reflects the major version of the referenced Autofac nuget package as below.

EgonsoftHU.Extensions.Logging.Autofac Autofac
4.0.0 - 4.0.1 4.9.4
5.0.0 - 5.0.1 5.2.0
6.0.0 6.3.0
6.0.1 6.4.0

You can find the release notes here.

Summary

These packages use Autofac features so that you can use the ILogger interface type for injection instead of ILogger<TCategoryName> interface type.

Instructions

First, determine which version of the Autofac nuget package you use. If you do not use it yet then I suggest using the latest (as of writing 6.4.0) version.

Next, install the latest version of the matching major version of the EgonsoftHU.Extensions.Logging.Autofac NuGet package.

.NET CLI:

dotnet add package EgonsoftHU.Extensions.Logging.Autofac --version 6.0.1

Package Manager:

Install-Package EgonsoftHU.Extensions.Logging.Autofac -Version 6.0.1

Next, add ConfigureContainer<ContainerBuilder>() to the Generic Host in CreateHostBuilder().

using Autofac;
using Autofac.Extensions.DependencyInjection;

namespace YourCompany.YourProduct.WebApi
{
    public class Program
    {
        // rest omitted for clarity

        static IHostBuilder CreateHostBuilder(string[] args)
        {
            return
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(
                        webHostBuilder =>
                        {
                            webHostBuilder.UseStartup<Startup>();
                        }
                    )
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureContainer<ContainerBuilder>( // <-- Add this method call
                        builder =>
                        {
                            // here comes the magic
                        }
                    );
        }
    }
}

Alternatively, you can add ConfigureContainer(ContainerBuilder builder) to your Startup.cs file.

using Autofac;

using EgonsoftHU.Extensions.DependencyInjection.Autofac;

namespace YourCompany.YourProduct.WebApi
{
    public class Startup
    {
        // rest omitted for clarity.

        public void ConfigureContainer(ContainerBuilder builder) // <-- Add this method
        {
            // here comes the magic
        }
    }
}

Finally, replace the // here comes the magic comment with one of the usage options.

Usage option #1 - Register the module explicitly

builder.RegisterModule<EgonsoftHU.Extensions.Logging.Autofac.DependencyModule>();

Usage option #2 - Register the module implicitly by using EgonsoftHU.Extensions.DependencyInjection.Autofac nuget package

// Step #1: Configure assembly registry
//
// Add nameof(EgonsoftHU) as an additional assembly file name prefix.
builder.UseDefaultAssemblyRegistry(nameof(YourCompany), nameof(EgonsoftHU));

// Step #2: Register the module that will discover and register all other modules.
//
// This will register EgonsoftHU.Extensions.Logging.Autofac.DependencyModule as well.
builder.RegisterModule<EgonsoftHU.Extensions.DependencyInjection.Autofac.DependencyModule>();

Click here to learn more about the EgonsoftHU.Extensions.DependencyInjection.Autofac nuget package.

About

A dependency module (derived from Autofac.Module) that enables injecting the same contextual logger as ILogger instead of ILogger<TCategoryName>

Topics

Resources

License

Stars

Watchers

Forks

Languages