Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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();
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda