Ir al contenido

Logging in constructor (ARCH1007)

Esta página aún no está disponible en tu idioma.

Key dotnet.logging.logging-in-constructor · Severity info · Category logging

Logging inside a constructor burdens object creation with side effects and produces noise across the DI lifecycle. Move logging into actual work methods.

public OrderService(ILogger<OrderService> logger)
{
_logger = logger;
_logger.LogInformation("OrderService created");
}
public OrderService(ILogger<OrderService> logger)
{
_logger = logger;
}

Remove the log call from the constructor; if it is genuinely needed, move it to the relevant work method or an initialization step.