This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: csharp-best-practices/logger-constant-template

Language: C#

Severity: Info

Category: Best Practices

Description

Using interpolated strings ($"...") or string concatenation forces the CLR to build a new string every time the log statement executes — even if the current log level is disabled (e.g., when logging at Debug in production). Prefer using constant templates when logging data.

Non-Compliant Code Examples

_logger.LogInformation("User " + userId + " logged in");
_logger.LogInformation($"User {userId} logged in at {timestamp}");

Compliant Code Examples

// check log enablement before
if (_logger.IsEnabled(LogLevel.Debug)) {
    _logger.LogInformation("User " + userId + " logged in");
}
_logger.LogInformation("User {UserId} logged in at {Timestamp}", userId, timestamp);
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains