Logging Source Generator fails with a InvalidCastException
theit8514 opened this issue · 5 comments
Description
Adding a SuppressMessageAttribute to a method marked with LoggerMessageAttribute results in a failed compilation
Reproduction Steps
- Create a new Class Library
- Add
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.1" /> - 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);
}
}- 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
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
- Create a new Class Library
- Add
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.1" /> - 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);
}
}- 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: |
|
| Milestone: | - |
@theit8514 would you please try this with the newer preview version?
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)
Thanks will take a look
@theit8514 thanks for the report, the fix should be available for preview 4.