Logging in constructor (ARCH1007)
Ce contenu n’est pas encore disponible dans votre langue.
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.
Incorrect
Section titled “Incorrect”public OrderService(ILogger<OrderService> logger){ _logger = logger; _logger.LogInformation("OrderService created");}Correct
Section titled “Correct”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.