Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value |
|---|---|
| Rule ID | CA2023 |
| Title | Invalid braces in message template |
| Category | Reliability |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 9 | As warning |
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.
Rule description
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.
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
The following code snippet shows violations of CA2023:
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);
}
}
Imports Microsoft.Extensions.Logging
Class Example
Private ReadOnly _logger As ILogger
Public Sub New(logger As ILogger(Of Example))
_logger = logger
End Sub
Public Sub LogData(name As String, value As Integer)
' 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)
End Sub
End Class
The following code snippet fixes the violations:
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);
}
}
Imports Microsoft.Extensions.Logging
Class Example
Private ReadOnly _logger As ILogger
Public Sub New(logger As ILogger(Of Example))
_logger = logger
End Sub
Public Sub LogData(name As String, value As Integer)
' 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)
End Sub
End Class
When to suppress warnings
Don't suppress a warning from this rule. Invalid braces in message templates can cause run-time exceptions or incorrect log output.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2023
// The code that's violating the rule is on this line.
#pragma warning restore CA2023
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2023.severity = none
For more information, see How to suppress code analysis warnings.