This product is not supported for your selected Datadog site. ().
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

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

シームレスな統合。 Datadog Code Security をお試しください