Skip to content

Logging Source Generator fails with a InvalidCastException #67167

@theit8514

Description

@theit8514

Description

Adding a SuppressMessageAttribute to a method marked with LoggerMessageAttribute results in a failed compilation

Reproduction Steps

  1. Create a new Class Library
  2. Add <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.1" />
  3. Write the following code in Class1.cs
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;

namespace ClassLibrary1
{
    public partial class Class1
    {
        private ILogger _logger;
        public Class1(ILogger logger) => _logger = logger;

        [LoggerMessage(EventId = 1, Level = LogLevel.Error, Message = "Log Message {Name}")]
        [SuppressMessage("ReSharper", "InconsistentNaming")]
        private partial void LogError(string Name, Exception exception);
    }
}
  1. Build

Expected behavior

The source generator should generate.

Actual behavior

The source generator fails with an error in the build log:

CSC : warning CS8785: Generator 'LoggerMessageGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidCastException' with message 'Unable to cast object of type 'System.String' to type 'System.Int32'.'

Regression?

No response

Known Workarounds

No response

Configuration

dotnet SDK version: 6.0.103
OS: Manjaro Linux (x64)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions