Search code examples
c#.netloggingdependency-injectionilogger

Should private class member use its own ILogger instance?


I'm writing a C# library, and am trying to figure out how best to do logging. I want my library to be useable with and without DI.

Suppose my libary has one main public class called Foo which accepts an IBar dependency via its constructor, but also has a hard-wired private member of type Qux (an internal class).

To keep my library logging-framework agnostic, I believe the best practice is to pass an ILogger<Foo> to the constructor of Foo, and an ILogger<BarImpl> to an implementation of IBar.

My question is, should Qux use the ILogger<Foo> logger, or should it have its own logger, ILogger<Qux>? If so, how would Foo create an ILogger<Qux> to pass to Qux?

public interface IBar {}

public class BarImpl : IBar
{
    public BarImpl(ILogger<BarImpl> logger)
    {
    }
}

internal class Qux
{
    public Qux(ILogger<Qux> logger) // should Qux accept ILogger<Qux> or ILogger<Foo>?
    {
    }
}

public class Foo
{
    private Qux _qux;
    
    public Foo(IBar bar, ILogger<Foo> logger)
    {
        // how to create ILogger<Qux> here?
        // _qux = new Qux();
    }
}

Solution

  • You have a few ways of doing this.

    1. If you'd like Qux to be an implementation detail of Foo as it is now, then:
    public Foo(IBar bar, ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Foo>();
        _qux = new Qux(loggerFactory.CreateLogger<Qux>());
    }
    
    1. If you want to use DI more correctly, and can depend on users correctly using your library (as suggested by @Llama):
    public Foo(IBar bar, Qux qux, ILogger<Foo> logger)
    {
        _logger = logger;
        _qux = qux;
    }
    
    // inside your library where you can see the internal Qux
    public static IServiceCollection InjectMyLibraryServices(this IServiceCollection services)
    {
        // ...
        services.AddScoped<IQux, Qux>();
        services.AddScoped<IFoo, Foo>();
    }
    
    1. You can get rid of DI for Qux and just get the logger (as mentioned by @canton7):
    public Foo(IBar bar, ILogger<Foo> logger)
    {
        _logger = logger;
        _qux = new Qux();
    }
    
    internal class Qux 
    {
        private readonly ILogger _logger = LogManager.GetLogger(typeof(Qux));
    }
    

    Side note: this last approach represents the Service Locator anti-pattern and hides the logging dependency. Use only if you understand the pros and cons of doing things this way. I personally wouldn't recommend this approach.