Zum Inhalt springen

Logging in constructor (ARCH1007)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.