-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 to compile using keyword parameters with @ prefixes #60968
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsDescriptionIf a C# keyword is used as a parameter name for a [LoggerMessage(1, LogLevel.Information, "Event: {event}")]
public static partial void LogEvent(ILogger logger, object @event); If the Reproduction StepsAttempt 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 behaviorThe following text is printed to the console:
Actual behaviorThe application fails to compile:
It also fails to compile if the code is changed to include the
Regression?No. Known WorkaroundsDo not use a C# identifier prefixed with ConfigurationOutput from
Other informationNo response
|
I had similar issue. Seems that message template parser fails to recognize special meaning of "@" where it's well-known standard: Tested with |
moreover it seems that @ is generally not supported well MS.Extensions.Logging using Serilog;
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
Log.Logger.Warning("Person {Person}", new Person("Mike", 30));
//ToString is called: Person Person { Name = Mike, Age = 30 }
Log.Logger.Warning("Person properties {@Person}", new Person("Mike", 30));
//properties are being unpacked: Person properties {"Name": "Mike", "Age": 30, "$type": "Person"} |
Related issue: serilog/serilog-extensions-logging#197 |
@maryamariyan can this issue be addresses in current version or we need to wait till NET 7.0 ? |
@maryamariyan , I'm ready fix this in two steps:
|
This would be fixed in 7.0 |
Closed #62572 as dupe of this. Would also need to consider support for parameter name prefixed with $ as mentioned in that issue. |
…ters (dotnet#64663) * Adds support to `@` signed prefixed parameters Fixes dotnet#60968 * Move repetitive logic to a new property * Remove NeedsAtSign
The bugfix was included in the main branch for 7.0. As part of PR #64779, we're also considering this for servicing for 6.0. |
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#.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:
Expected behavior
The following text is printed to the console:
Actual behavior
The application fails to compile:
It also fails to compile if the code is changed to include the
@
in the template:Regression?
No.
Known Workarounds
Do not use a C# identifier prefixed with
@
as a logger message method parameter.Configuration
Output from
dotnet --info
:Other information
No response
The text was updated successfully, but these errors were encountered: