A message template must conform to the specification. The rule raises an issue if the template string violates the template string grammar.
A message template needs to comply with a set of rules. Logging provider parse the template and enrich log entries with the information found in the template. An unparsable message template leads to corrupted log entries and might result in a loss of information in the logs.
The rule covers the following logging frameworks:
Follow the syntax described on https://messagetemplates.org/.
logger.LogError("Login failed for {User", user); // Noncompliant: Syntactically incorrect
logger.LogError("Login failed for {}", user); // Noncompliant: Empty placeholder
logger.LogError("Login failed for {User-Name}", user); // Noncompliant: Only letters, numbers, and underscore are allowed for placeholders
logger.LogDebug("Retry attempt {Cnt,r}", cnt); // Noncompliant: The alignment specifier must be numeric
logger.LogDebug("Retry attempt {Cnt:}", cnt); // Noncompliant: Empty format specifier is not allowed
logger.LogError("Login failed for {User}", user);
logger.LogError("Login failed for {User}", user);
logger.LogError("Login failed for {User_Name}", user);
logger.LogDebug("Retry attempt {Cnt,-5}", cnt);
logger.LogDebug("Retry attempt {Cnt:000}", cnt);
Serilog002:
Message template syntax verifier