Skip to content

Logging Source Generator fails to compile using keyword parameters with @ prefixes #60968

Description

@martincostello

Description

If a C# keyword is used as a parameter name for a [LoggerMessage] method using the logging source generator prefixed with an @, such as the example below, the application will fail to compile due to the source generator creating invalid C#.

[LoggerMessage(1, LogLevel.Information, "Event: {event}")]
public static partial void LogEvent(ILogger logger, object @event);

If the @ is included in the template to match the parameter, it will also fail to compile.

Reproduction Steps

Attempt to run the following program using .NET 6 RC2:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0-rc.2.21480.5" /> 
    <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-rc.2.21480.5" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0-rc.2.21480.5" />
  </ItemGroup>
</Project>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using var serviceProvider = new ServiceCollection()
    .AddLogging(builder => builder.AddConsole())
    .BuildServiceProvider();

var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("MyLogger");

Log.LogEvent(logger, new { foo = "bar" });

internal static partial class Log
{
    [LoggerMessage(1, LogLevel.Information, "Event: {event}")]
    public static partial void LogEvent(ILogger logger, object @event);
}

Expected behavior

The following text is printed to the console:

info: MyLogger[1]
      Event: { foo = bar }

Actual behavior

The application fails to compile:

C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,125): error CS0065: 'Log.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(15,49): error CS0065: 'Log.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,36): error CS0756: A partial method may not have multiple defining declarations [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Program.cs(16,32): error CS8795: Partial method 'Log.LogEvent(ILogger, object)' must have an implementation part because it has accessibility modifiers. [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,36): error CS0111: Type 'Log' already defines a member called 'LogEvent' with the same parameter types [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(15,49): error CS0102: The type 'Log' already contains a definition for '' [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,125): error CS0708: '': cannot declare instance members in a static class [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(15,49): error CS0708: '': cannot declare instance members in a static class [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]

The build failed. Fix the build errors and run again

It also fails to compile if the code is changed to include the @ in the template:

C:\Coding\martincostello\LoggerMessageKeywords\Program.cs(16,64): warning SYSLIB1015: Argument 'event' is not referenced from the logging message [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Program.cs(15,6): error SYSLIB1014: Template '@event' is not provided as argument to the logging method [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(50,125): error CS0065: 'Log.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(57,47): error CS0065: 'Log.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(50,36): error CS0756: A partial method may not have multiple defining declarations [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Program.cs(16,32): error CS8795: Partial method 'Log.LogEvent(ILogger, object)' must have an implementation part because it has accessibility modifiers. [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,20): error CS0501: 'Log.__LogEventStruct.__LogEventStruct(object)' must declare a body because it is not marked abstract, extern, or partial [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(11,64): error CS0065: 'Log.__LogEventStruct.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(13,36): error CS0065: 'Log.__LogEventStruct.': event property must have both add and remove accessors [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(50,36): error CS0111: Type 'Log' already defines a member called 'LogEvent' with the same parameter types [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(57,47): error CS0102: The type 'Log' already contains a definition for '' [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(50,125): error CS0708: '': cannot declare instance members in a static class [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(57,47): error CS0708: '': cannot declare instance members in a static class [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]
C:\Coding\martincostello\LoggerMessageKeywords\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(13,36): error CS0102: The type 'Log.__LogEventStruct' already contains a definition for '' [C:\Coding\martincostello\LoggerMessageKeywords\LoggerMessageKeywords.csproj]

The build failed. Fix the build errors and run again.

Regression?

No.

Known Workarounds

Do not use a C# identifier prefixed with @ as a logger message method parameter.

Configuration

Output from dotnet --info:

.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.2.21505.57
 Commit:    ab39070116

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.2.21505.57\

Host (useful for support):
  Version: 6.0.0-rc.2.21480.5
  Commit:  6b11d64e7e

Other information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions