-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add support for primary constructors in LoggerMessageGenerator #101660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d7497fc
15643ec
9fc6ce4
2d6748c
6a8b473
6cf2b2f
69a5c00
e029d54
fd480f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -632,6 +632,30 @@ private static string GenerateClassName(TypeDeclarationSyntax typeDeclaration) | |
|
|
||
| bool onMostDerivedType = true; | ||
|
|
||
| #if ROSLYN4_8_OR_GREATER | ||
kimsey0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| IEnumerable<IMethodSymbol> primaryConstructors = classType.InstanceConstructors | ||
| .Where(ic => ic.DeclaringSyntaxReferences | ||
| .Any(ds => ds.GetSyntax() is ClassDeclarationSyntax)); | ||
|
|
||
| foreach (IMethodSymbol primaryConstructor in primaryConstructors) | ||
|
||
| { | ||
| foreach (IParameterSymbol parameter in primaryConstructor.Parameters) | ||
| { | ||
| if (IsBaseOrIdentity(parameter.Type, loggerSymbol)) | ||
| { | ||
| if (loggerField == null) | ||
| { | ||
| loggerField = parameter.Name; | ||
| } | ||
| else | ||
| { | ||
| return (null, true); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| while (classType is { SpecialType: not SpecialType.System_Object }) | ||
| { | ||
| foreach (IFieldSymbol fs in classType.GetMembers().OfType<IFieldSymbol>()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
kimsey0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <PropertyGroup> | ||
| <AnalyzerRoslynVersion>4.8</AnalyzerRoslynVersion> | ||
| <RoslynApiVersion>$(MicrosoftCodeAnalysisVersion_4_8)</RoslynApiVersion> | ||
| <DefineConstants>$(DefineConstants);ROSLYN4_0_OR_GREATER;ROSLYN4_4_OR_GREATER;ROSLYN4_8_OR_GREATER</DefineConstants> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="Microsoft.Extensions.Logging.Generators.targets" /> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" /> | ||
| <Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Remove="LoggerMessageGenerator.Roslyn3.11.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // <auto-generated/> | ||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses | ||
| { | ||
| partial class TestWithLoggerFromPrimaryConstructor | ||
| { | ||
| [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")] | ||
| private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __M0Callback = | ||
| global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Debug, new global::Microsoft.Extensions.Logging.EventId(0, nameof(M0)), "M0", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); | ||
|
|
||
| [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")] | ||
| public partial void M0() | ||
| { | ||
| if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Debug)) | ||
| { | ||
| __M0Callback(logger, null); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <RoslynApiVersion>$(MicrosoftCodeAnalysisVersion_4_8)</RoslynApiVersion> | ||
| <DefineConstants>$(DefineConstants);ROSLYN4_0_OR_GREATER;ROSLYN4_8_OR_GREATER</DefineConstants> | ||
| <IsHighAotMemoryUsageTest>true</IsHighAotMemoryUsageTest> | ||
| <EmccLinkOptimizationFlag Condition="'$(ContinuousIntegrationBuild)' == 'true'">-O1</EmccLinkOptimizationFlag> | ||
| <!-- this Roslyn version brings in NS1.x dependencies --> | ||
| <FlagNetStandard1XDependencies>false</FlagNetStandard1XDependencies> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <HighAotMemoryUsageAssembly Include="Microsoft.CodeAnalysis.CSharp.dll" /> | ||
| </ItemGroup> | ||
|
|
||
| <Import Project="Microsoft.Extensions.Logging.Generators.targets" /> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\gen\Microsoft.Extensions.Logging.Generators.Roslyn4.8.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.