-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Description
In .NET 11, the LoggerMessage source generator has been refactored to support proper incremental building, significantly improving source generator performance by avoiding full re-runs on every compilation change. As a side effect of this refactoring, the generator now uses DiagnosticInfo.GetTrimmedLocation which creates diagnostic locations without SyntaxTree references. This breaks #pragma warning disable matching for diagnostics emitted by the logging source generator (e.g., SYSLIB1002, SYSLIB1013, SYSLIB1018). The System.Text.Json source generator already had this same limitation.
This change was introduced in dotnet/runtime#124077. For more context on why #pragma directive suppression is incompatible with incremental source generator builds, see dotnet/runtime#92509 and dotnet/roslyn#68291.
Version
.NET 11 Preview 2
Previous behavior
Users could use #pragma warning disable directives in source code to suppress diagnostics emitted by the LoggerMessage source generator, such as:
#pragma warning disable SYSLIB1002
#pragma warning disable SYSLIB1013
#pragma warning disable SYSLIB1018The compiler would match the pragma directives to the diagnostic locations and suppress them as expected.
New behavior
#pragma warning disable directives no longer suppress diagnostics emitted by the LoggerMessage source generator. The diagnostics are still reported but the compiler cannot match them to pragma directives because the diagnostic locations are created without SyntaxTree references (via DiagnosticInfo.GetTrimmedLocation).
This aligns the LoggerMessage behavior with the other source generators like System.Text.Json source generator, which already had this same limitation.
Type of breaking change
- Binary incompatible: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
- Behavioral change: Existing binaries might behave differently at run time.
Reason for change
The change was made to improve the incrementality of the LoggerMessage source generator. Previously, the generator was passing Compilation through its pipeline, causing full re-runs on every compilation change rather than only when attributed syntax nodes changed. The refactoring to follow proper incremental generator patterns involved using DiagnosticInfo with trimmed locations (without SyntaxTree references) as a side effect.
See dotnet/runtime#124077 and dotnet/runtime#76119 for details.
Recommended action
Use project-level <NoWarn> in MSBuild instead of #pragma warning disable directives to suppress LoggerMessageGenerator diagnostics:
<PropertyGroup>
<NoWarn>$(NoWarn);SYSLIB1002;SYSLIB1013;SYSLIB1018</NoWarn>
</PropertyGroup>Alternatively, use the .editorconfig file to suppress diagnostics:
[*.cs]
dotnet_diagnostic.SYSLIB1002.severity = none
dotnet_diagnostic.SYSLIB1013.severity = none
dotnet_diagnostic.SYSLIB1018.severity = none
Feature area
Extensions
Affected APIs
Microsoft.Extensions.Logging.LoggerMessageAttribute
Metadata
Metadata
Assignees
Labels
Type
Projects
Status