org.sonar.plugins.csharp.S6677.html Maven / Gradle / Ivy
Why is this an issue?
Named placeholders in message templates should be unique. The meaning of the named placeholders is to
store the value of the provided argument under that name, enabling easier log querying. Since the named placeholder is used multiple times, it cannot
store the different values uniquely with each name hence not serving its original purpose. There can be different behaviours when using the same named
placeholder multiple times:
- Microsoft.Extensions.Logging saves the different values under the
same name
- Serilog stores only the latest assigned value
- Nlog makes the name unique by suffixing it with
_index
The rule covers the following logging frameworks:
How to fix it
Assign unique names to each template placeholder.
Code examples
Noncompliant code example
public void Checkout(ILogger logger, User user, Order order)
{
logger.LogDebug("User {Id} purchased order {Id}", user.Id, order.Id);
}
Compliant solution
public void Checkout(ILogger logger, User user, Order order)
{
logger.LogDebug("User {UserId} purchased order {OrderId}", user.Id, order.Id);
}
Resources
Documentation
- Message Templates - Message template specification
- Microsoft Learn - Log message template
formatting
- NLog - How to use structured logging
- Serilog - Structured Data
- Serilog -
Serilog005
: Unique
Property Name Verifier
© 2015 - 2024 Weber Informatics LLC | Privacy Policy