A log message template has a different number of placeholders than arguments

tx0/csharp:log-template-argumentsrecommendedwarningreliability/api-misusedepth fastbug

What it finds

logger.LogInformation("Order {Id} for {Customer}", id) fills the placeholders by position: with too few arguments the logger fails or writes the template unfilled, and with too many the extra values are dropped. The calls checked are Log, LogTrace, LogDebug, LogInformation, LogWarning, LogError and LogCritical with a literal template; an exception, event id or log level before the template is skipped.

From the rule's source, log_template_arguments.rs.

Example from the rule's tests
using System;using Microsoft.Extensions.Logging; class Orders{    private readonly ILogger logger;     void Place(int id, string customer, Exception error)    {        logger.LogInformation("Order {Id} for {Customer}", id);tx0 reports: the message template has 2 placeholders but 1 arguments are passed        logger.LogError(error, "Order {Id} failed", id);        logger.LogWarning("Retrying {Id}", id, customer);tx0 reports: the message template has 1 placeholders but 2 arguments are passed        logger.LogDebug("No placeholders");    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:log-template-arguments" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:log-template-arguments] reason="..."

tx0 explain tx0/csharp:log-template-arguments shows the same in the terminal. Found a false positive? Report it.