All rules
CA2023Reliability Enabled by default: As warning
Invalid braces in message template
Invalid braces in message template
Microsoft docsDescription
Logging message templates use curly braces { and } to denote named placeholders for values. Invalid brace usage in message templates can result in runtime exceptions or unexpected logging behavior. This rule detects:
- Unmatched opening or closing braces.
- Nested braces that aren't properly escaped.
- Other malformed brace patterns.
Cause
The braces present in the message template are invalid. Ensure any braces in the message template are valid opening/closing braces, or are escaped.
How to fix violations
To fix a violation of this rule:
- Ensure all opening braces
{have a corresponding closing brace}. - Escape literal braces by doubling them:
{{for{and}}for}. - Fix any nested or malformed brace patterns.
Example
using Microsoft.Extensions.Logging;
class Example
{
private readonly ILogger _logger;
public Example(ILogger<Example> logger)
{
_logger = logger;
}
public void LogData(string name, int value)
{
// Violation: unmatched opening brace.
_logger.LogInformation("Processing {Name with value {Value}", name, value);
// Violation: unmatched closing brace.
_logger.LogInformation("Processing Name} with value {Value}", name, value);
}
}
using Microsoft.Extensions.Logging;
class Example
{
private readonly ILogger _logger;
public Example(ILogger<Example> logger)
{
_logger = logger;
}
public void LogData(string name, int value)
{
// Fixed: proper braces.
_logger.LogInformation("Processing {Name} with value {Value}", name, value);
// Fixed: escaped literal braces.
_logger.LogInformation("Processing {{Name}} with value {Value}", name, value);
}
}When to suppress
Don't suppress a warning from this rule. Invalid braces in message templates can cause runtime exceptions or incorrect log output.
Your vote
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0