Logging source generator: support generic methods (lift SYSLIB1011)#124638
Open
Logging source generator: support generic methods (lift SYSLIB1011)#124638
Conversation
…restriction) Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Lift restriction on generic methods for logging
Logging source generator: support generic methods (lift SYSLIB1011)
Feb 20, 2026
svick
reviewed
Feb 20, 2026
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
svick
reviewed
Feb 20, 2026
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
svick
reviewed
Feb 20, 2026
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: svick <287848+svick@users.noreply.github.com>
svick
reviewed
Feb 20, 2026
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs
Show resolved
Hide resolved
…struct; handle nullable constraints Co-authored-by: svick <287848+svick@users.noreply.github.com>
svick
reviewed
Feb 20, 2026
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
…; fix XLF states Co-authored-by: svick <287848+svick@users.noreply.github.com>
Co-authored-by: svick <287848+svick@users.noreply.github.com>
Copilot stopped work on behalf of
svick due to an error
February 20, 2026 16:03
This was referenced Feb 21, 2026
…arameter type name display call Co-authored-by: svick <287848+svick@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Enables the LoggerMessage source generator to support generic logging methods by carrying method type parameters/constraints through parsing, caching, and emission, while narrowing SYSLIB1011 to only forbid the C# 13 allows ref struct anti-constraint.
Changes:
- Lifted the blanket rejection of generic logging methods; added parsing/serialization of method type parameters + constraint rendering (including nullable constraints).
- Updated emitter to generate generic state structs and generic partial method signatures (and to exclude generic methods from the
LoggerMessage.Definefast-path). - Updated
SYSLIB1011descriptor/message/resources/docs and added/updated parser + emitter baseline tests.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs | Parses method type parameters/constraints; diagnoses allows ref struct; preserves display format options. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs | Emits generic structs/methods/constraints; avoids Define path for generic methods; small perf cleanup. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Roslyn4.0.cs | Round-trips method type parameter data through the incremental cache. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs | Renames/narrows SYSLIB1011 descriptor to the allows ref struct restriction. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/Strings.resx | Updates SYSLIB1011 message text and resource key. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.cs.xlf | Updates localized resource key/message for SYSLIB1011 (marked needs-translation). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.de.xlf | Same as above (German). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.es.xlf | Same as above (Spanish). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.fr.xlf | Same as above (French). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.it.xlf | Same as above (Italian). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ja.xlf | Same as above (Japanese). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ko.xlf | Same as above (Korean). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pl.xlf | Same as above (Polish). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.pt-BR.xlf | Same as above (Portuguese - Brazil). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.ru.xlf | Same as above (Russian). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.tr.xlf | Same as above (Turkish). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hans.xlf | Same as above (Chinese - Simplified). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Resources/xlf/Strings.zh-Hant.xlf | Same as above (Chinese - Traditional). |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs | Adds/updates tests for generic methods, constraints, nullable constraints, and allows ref struct diagnostic. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs | Adds emitter baseline test covering generic methods. |
| src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithGenericMethods.generated.txt | New baseline output verifying correct generic codegen. |
| docs/project/list-of-diagnostics.md | Updates SYSLIB1011 documentation to reflect the narrowed restriction. |
...ons/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs
Show resolved
Hide resolved
…d instead of hard-coded string Co-authored-by: svick <287848+svick@users.noreply.github.com>
svick
approved these changes
Feb 23, 2026
Member
|
The equivalent PR in the telemetry logging source generator is at dotnet/extensions#7331. |
Co-authored-by: svick <287848+svick@users.noreply.github.com>
tarekgh
approved these changes
Feb 23, 2026
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.
Generic
[LoggerMessage]methods were unconditionally rejected withSYSLIB1011. Methods with type parameters are fully supportable via the struct-based code generation path. The only remaining restriction isallows ref struct(C# 13), since the generated struct stores params in fields and cannot hold ref-struct type arguments.Description
Parser (
LoggerMessageGenerator.Parser.cs)method.Arity > 0→SYSLIB1011checkLoggerMethodTypeParameter/LoggerMethodTypeParameterSpecrecord types to carry type parameter names + constraints through the pipelineGetTypeParameterConstraintsserializes all constraint kinds:class/class?,struct,unmanaged,notnull, base/interface types (incl. nullable),new()FullyQualifiedFormat.MiscellaneousOptions(EscapeKeywordIdentifiers | UseSpecialTypes) when addingIncludeNullableReferenceTypeModifier— fixes a pre-existing bug on the same call for parameter typesIErrorTypeSymbolconstraint types (unresolvable in generated code)allows ref structdetected viaFunc<ITypeParameterSymbol, bool>delegate compiled fromITypeParameterSymbol.AllowsRefLikeTypeviaDelegate.CreateDelegate(compiles against all supported Roslyn versions; no per-call boxing) — emitsSYSLIB1011DiagnosticDescriptors.LoggingMethodIsGeneric→LoggingMethodHasAllowsRefStructConstraint; updated message + all XLF files (only changed entry markedneeds-translation)Emitter (
LoggerMessageGenerator.Emitter.cs)UseLoggerMessageDefineexcludes generic methods — theDefinepath caches a static delegate and cannot capture method type parametersGenStructemits the state struct as generic (__M1Struct<T>) with matchingwhereconstraintsGenLogMethodemits<T, U>andwhereclauses on the partial method signatureGenHolder/Formatreferences parameterized with concrete type argumentsGetTypeParameterList/GetTypeConstraints(string-returning, allocating) withGenTypeParameterList/GenTypeConstraintswriting directly to_builderusing System.Linq;IndexOf('{')replacesContains('{')(no allocating LINQ path on netstandard2.0)Incremental cache (
LoggerMessageGenerator.Roslyn4.0.cs)FromSpecround-tripsTypeParametersthrough the incremental cacheDocs
docs/project/list-of-diagnostics.mdupdated: SYSLIB1011 now describes theallows ref structrestrictionExample
All constraint forms supported:
class,class?,struct,unmanaged,notnull, base type, interface, nullable interface,new().allows ref structemits SYSLIB1011.Original prompt
SYSLIB1011should be lifted) #90589💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.