Skip to content

Don't show SYSLIB1015 when no template given - #131878

Open
rosebyte wants to merge 1 commit into
dotnet:mainfrom
rosebyte:rosebyte-fix-syslib1015-without-message-template
Open

Don't show SYSLIB1015 when no template given#131878
rosebyte wants to merge 1 commit into
dotnet:mainfrom
rosebyte:rosebyte-fix-syslib1015-without-message-template

Conversation

@rosebyte

@rosebyte rosebyte commented Aug 5, 2026

Copy link
Copy Markdown
Member

When a [LoggerMessage] logging method supplies no message, the source generator reported
SYSLIB1015: Argument '<name>' is not referenced from the logging message once per parameter.
Since SYSLIB1015 is a warning, any project using TreatWarningsAsErrors fails to build.

[LoggerMessage(LogLevel.Critical)]
public static partial void FoodRecallNotice(
    this ILogger logger,
    string brandName, string productDescription, string productType,
    string recallReasonDescription, string companyName);
Program.cs(18,16): warning SYSLIB1015: Argument 'brandName' is not referenced from the logging message
Program.cs(19,16): warning SYSLIB1015: Argument 'productDescription' is not referenced from the logging message
Program.cs(20,16): warning SYSLIB1015: Argument 'productType' is not referenced from the logging message
Program.cs(21,16): warning SYSLIB1015: Argument 'recallReasonDescription' is not referenced from the logging message
Program.cs(22,16): warning SYSLIB1015: Argument 'companyName' is not referenced from the logging message

The diagnostic is simply mis-scoped. Its own text asserts that an argument is not referenced
from the logging message, which is vacuous when there is no message. Logging without a message,
capturing the parameters as structured state only, is a legitimate and widely used pattern; the
arguments are still recorded correctly and reach structured sinks intact. The equivalent diagnostic
in Microsoft.Extensions.Telemetry.Abstractions (LOGGEN011) is Info rather than Warning,
which is why the same code compiles cleanly against that generator.

Change

LoggerMessageGenerator.Parser.cs now computes hasMessage once per logging method and guards
both ArgumentHasNoCorrespondingTemplate call sites with it. No diagnostic ID, severity, or
resource string is changed, and behaviour is untouched wherever a message exists.

Scenario Before After
No message, 5 parameters 5 × SYSLIB1015 none
Message = "" or Message = null 1 per parameter none
Message = "Recall for {foo}", params foo, bar, baz 2, naming bar and baz unchanged
Message = "Recall issued" (no placeholders), param foo 1, naming foo unchanged
Message = " " (whitespace), param foo 1, naming foo unchanged

Re-enabled tests

MessageTestExtensions.M2/M3 and their assertions in LoggerMessageGeneratedCodeTests were
written years ago for exactly this scenario and disabled under #if false precisely because they
emitted these unsuppressable warnings. They now compile cleanly and are enabled, which gives the
pattern its first end-to-end runtime coverage; previously no test asserted that a message-less
method with parameters produces correct state. M4 (Message = "{p1}" with unreferenced p2,
p3) still warns by design and remains disabled under the existing [ActiveIssue].

Copilot AI review requested due to automatic review settings August 5, 2026 14:42
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the LoggerMessage source generator to avoid reporting SYSLIB1015 (“Argument … is not referenced from the logging message”) when a logging method has no message template, and re-enables/adds tests to cover message-less logging methods that still capture parameters as structured state.

Changes:

  • Update LoggerMessageGenerator.Parser to compute hasMessage once per method and guard ArgumentHasNoCorrespondingTemplate diagnostics on it.
  • Re-enable and extend generator/runtime tests for message-less logging methods (including null/empty message cases).
  • Clarify LoggerMessageAttribute.Message XML docs to describe message-less structured-state logging behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs Gate template-mismatch diagnostics on whether a non-empty message exists.
src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs Add coverage for null/empty/omitted messages and whitespace-message behavior.
src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/TestClasses/MessageTestExtensions.cs Re-enable message-less test extension methods; keep warning-producing case disabled.
src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratedCodeTests.cs Re-enable runtime assertions for message-less methods; keep Roslyn issue-gated test disabled.
src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessageAttribute.cs Document behavior when message is omitted/empty (structured state only).
Suppressed comments (2)

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs:146

  • Same issue as line 142: this InlineData verbatim string uses """ to represent an empty string, which actually ends the string literal. It should produce [LoggerMessage(0, LogLevel.Debug, "")] in the embedded source (empty message), e.g. via a raw string literal """[LoggerMessage(0, LogLevel.Debug, "")]""" (with "").
        [InlineData(@"[LoggerMessage(0, LogLevel.Debug, """")]")]

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs:163

  • These InlineData verbatim strings are not valid C#: @"[LoggerMessage("""")]" ends the string literal early. Please switch them to compile-time constants that correctly represent an empty string in the embedded attribute source, e.g. raw string literals like """[LoggerMessage("")]""" and """[LoggerMessage(LogLevel.Debug, "")]""" (with "").
        [InlineData(@"[LoggerMessage("""")]")]
        [InlineData(@"[LoggerMessage(LogLevel.Debug, null)]")]
        [InlineData(@"[LoggerMessage(LogLevel.Debug, """")]")]

Comment on lines +108 to +111
/// <remarks>
/// When omitted or empty, no message is formatted and the logging method's parameters are
/// captured as structured state only.
/// </remarks>

[Theory]
[InlineData(@"[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = null)]")]
[InlineData(@"[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = """")]")]
Comment on lines 190 to +192
Assert.Single(diagnostics);
Assert.Equal(DiagnosticDescriptors.ArgumentHasNoCorrespondingTemplate.Id, diagnostics[0].Id);
Assert.Contains("foo", diagnostics[0].GetMessage(), StringComparison.InvariantCulture);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants