Don't show SYSLIB1015 when no template given - #131878
Open
rosebyte wants to merge 1 commit into
Open
Conversation
|
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. |
Contributor
There was a problem hiding this comment.
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.Parserto computehasMessageonce per method and guardArgumentHasNoCorrespondingTemplatediagnostics on it. - Re-enable and extend generator/runtime tests for message-less logging methods (including null/empty message cases).
- Clarify
LoggerMessageAttribute.MessageXML 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a
[LoggerMessage]logging method supplies no message, the source generator reportedSYSLIB1015: Argument '<name>' is not referenced from the logging messageonce per parameter.Since SYSLIB1015 is a warning, any project using
TreatWarningsAsErrorsfails to build.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) isInforather thanWarning,which is why the same code compiles cleanly against that generator.
Change
LoggerMessageGenerator.Parser.csnow computeshasMessageonce per logging method and guardsboth
ArgumentHasNoCorrespondingTemplatecall sites with it. No diagnostic ID, severity, orresource string is changed, and behaviour is untouched wherever a message exists.
Message = ""orMessage = nullMessage = "Recall for {foo}", paramsfoo, bar, bazbarandbazMessage = "Recall issued"(no placeholders), paramfoofooMessage = " "(whitespace), paramfoofooRe-enabled tests
MessageTestExtensions.M2/M3and their assertions inLoggerMessageGeneratedCodeTestswerewritten years ago for exactly this scenario and disabled under
#if falseprecisely because theyemitted 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 unreferencedp2,p3) still warns by design and remains disabled under the existing[ActiveIssue].