Skip to content

Emit SelfLog warning when extra arguments are provided#2222

Merged
nblumhardt merged 1 commit into
serilog:devfrom
matantsach:fix/1600-selflog-extra-arguments
Feb 15, 2026
Merged

Emit SelfLog warning when extra arguments are provided#2222
nblumhardt merged 1 commit into
serilog:devfrom
matantsach:fix/1600-selflog-extra-arguments

Conversation

@matantsach

Copy link
Copy Markdown
Contributor

Summary

Fixes #1600

When a message template has no properties but arguments are supplied (e.g. logger.Information("Argument extra", 42)), no SelfLog diagnostic was emitted, making it difficult to detect mismatched log calls at runtime.

Changes

  • PropertyBinder.ConstructNamedProperties: Added a SelfLog.WriteLine call when NamedProperties is null but messageTemplateParameters.Length > 0
  • Added two unit tests:
    • ProvidingMoreParametersThanNamedPropertiesInTheTemplateWillSelfLog — verifies SelfLog fires when more args than named properties
    • ProvidingParametersWhenTemplateHasNoPropertiesWillSelfLog — verifies SelfLog fires for the exact scenario in No SelfLog event when there are extra arguments #1600

Performance

The added check (messageTemplateParameters.Length > 0) only executes in the early-return path when the template has no properties at all. On the normal hot path (templates with properties), there is zero overhead. This aligns with @jandresleiva's earlier benchmark analysis showing negligible impact.

Testing

All existing tests pass. Two new tests added covering both the reported scenario and the related "more args than properties" case.

When a message template has no properties but arguments are supplied
(e.g. `logger.Information("Message", 42)`), no SelfLog diagnostic
was emitted, making it difficult to detect mismatched log calls.

This adds a SelfLog.WriteLine call in ConstructNamedProperties when
the template has no named properties but parameters were provided.

Fixes serilog#1600
@nblumhardt

Copy link
Copy Markdown
Member

Looks great; thanks! (And thanks @jandresleiva for the earlier investigation 👍)

@nblumhardt
nblumhardt merged commit 60935b4 into serilog:dev Feb 15, 2026
1 check passed
@nblumhardt nblumhardt mentioned this pull request Jul 10, 2026
This was referenced Jul 10, 2026
This was referenced Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No SelfLog event when there are extra arguments

2 participants