Skip to content
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

Logging Source Generator fails with a InvalidCastException #67167

Closed
Tracked by #64015
theit8514 opened this issue Mar 25, 2022 · 5 comments · Fixed by #67179
Closed
Tracked by #64015

Logging Source Generator fails with a InvalidCastException #67167

theit8514 opened this issue Mar 25, 2022 · 5 comments · Fixed by #67179

Comments

@theit8514
Copy link

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

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Extensions-Logging untriaged New issue has not been triaged by the area owner labels Mar 25, 2022
@ghost
Copy link

ghost commented Mar 25, 2022

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: theit8514
Assignees: -
Labels:

untriaged, area-Extensions-Logging

Milestone: -

@maryamariyan
Copy link
Member

@theit8514 would you please try this with the newer preview version?

@theit8514
Copy link
Author

Tested with sdk version 7.0.100-preview.2.22153.17 and targeting net7.0 and referencing package Microsoft.Extensions.Logging.Abstractions version 7.0.0-preview.2.22152.2. Same build error.

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   7.0.100-preview.2.22153.17
 Commit:    9c52c56c13

Runtime Environment:
 OS Name:     manjaro
 OS Version:  
 OS Platform: Linux
 RID:         manjaro-x64
 Base Path:   /usr/share/dotnet/sdk/7.0.100-preview.2.22153.17/

Host (useful for support):
  Version: 7.0.0-preview.2.22152.2
  Commit:  e24f66dff0

.NET SDKs installed:
  6.0.103 [/usr/share/dotnet/sdk]
  7.0.100-preview.2.22153.17 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0-preview.2.22153.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0-preview.2.22152.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
$ dotnet build

Welcome to .NET 7.0!
---------------------
SDK Version: 7.0.100-preview.2.22153.17

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Microsoft (R) Build Engine version 17.2.0-preview-22152-03+719247ede for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
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'.' [/mnt/src/Test/ClassLibrary1/ClassLibrary1.csproj]
/mnt/src/Test/ClassLibrary1/Class1.cs(18,30): error CS8795: Partial method 'Class1.LogError(string, Exception)' must have an implementation part because it has accessibility modifiers. [/mnt/src/Test/ClassLibrary1/ClassLibrary1.csproj]

Build FAILED.

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'.' [/mnt/src/Test/ClassLibrary1/ClassLibrary1.csproj]
/mnt/src/Test/ClassLibrary1/Class1.cs(18,30): error CS8795: Partial method 'Class1.LogError(string, Exception)' must have an implementation part because it has accessibility modifiers. [/mnt/src/Test/ClassLibrary1/ClassLibrary1.csproj]
    1 Warning(s)
    1 Error(s)

@maryamariyan
Copy link
Member

Thanks will take a look

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Mar 25, 2022
@maryamariyan maryamariyan added this to the 7.0.0 milestone Mar 25, 2022
maryamariyan added a commit to maryamariyan/runtime that referenced this issue Mar 26, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Mar 26, 2022
maryamariyan added a commit that referenced this issue Mar 26, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 26, 2022
@maryamariyan
Copy link
Member

maryamariyan commented Mar 26, 2022

@theit8514 thanks for the report, the fix should be available for preview 4.

radekdoulik pushed a commit to radekdoulik/runtime that referenced this issue Mar 30, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 25, 2022
allantargino pushed a commit to allantargino/dotnet-runtime that referenced this issue Dec 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants