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 causes errors and warnings for messages using formatting specifiers #62572

Closed
dasMulli opened this issue Dec 9, 2021 · 2 comments
Labels
area-Extensions-Logging untriaged New issue has not been triaged by the area owner

Comments

@dasMulli
Copy link
Contributor

dasMulli commented Dec 9, 2021

Description

When using formatting specifiers in log message templates like {@Data} or {$Data}, the logging source generator logs warnings and errors but should compile fine.

Reproduction Steps

using Microsoft.Extensions.Logging;

public static partial class Extensions
{
    public static void LogPerson(this ILogger logger, Person person)
        => logger.LogInformation("The person is {@Person}", person);

    [LoggerMessage(Level = LogLevel.Information, Message = "The person is {@Person}")]
    public static partial void OtherLogPerson(this ILogger logger, Person person);
}

public record Person(string FirstName, string LastName)
{
    public override string ToString() => $"Person({FirstName} {LastName})";
}

Depending on the formatter used (NLog/Serilog/inbox console) there should be a difference when using {@Person} vs {Person} (in some cases {$Person})

Expected behavior

The code should compile without warnings or errors.

Actual behavior

SYSLIB1015 and SYSLIB1014 diagnostics and CS0119 error:

C:\demos\LoggingGeneratorFormatErrorExample\Class1.cs(9,75): warning SYSLIB1015: Argument 'person' is not referenced from the logging message [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]
C:\demos\LoggingGeneratorFormatErrorExample\Class1.cs(8,6): error SYSLIB1014: Template '@Person' is not provided as argument to the logging method [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]
C:\demos\LoggingGeneratorFormatErrorExample\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(20,41): error CS0119: 'Person' is a type, which is not valid in the given context [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]

Configuration

.NET 6 class library with 6.0.0 Microsoft.Extensions.Logging nuget pacakge

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

ghost commented Dec 9, 2021

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

Issue Details

Description

When using formatting specifiers in log message templates like {@Data} or {$Data}, the logging source generator logs warnings and errors but should compile fine.

Reproduction Steps

using Microsoft.Extensions.Logging;

public static partial class Extensions
{
    public static void LogPerson(this ILogger logger, Person person)
        => logger.LogInformation("The person is {@Person}", person);

    [LoggerMessage(Level = LogLevel.Information, Message = "The person is {@Person}")]
    public static partial void OtherLogPerson(this ILogger logger, Person person);
}

public record Person(string FirstName, string LastName)
{
    public override string ToString() => $"Person({FirstName} {LastName})";
}

Depending on the formatter used (NLog/Serilog/inbox console) there should be a difference when using {@Person} vs {Person} (in some cases {$Person})

Expected behavior

The code should compile without warnings or errors.

Actual behavior

SYSLIB1015 and SYSLIB1014 diagnostics:

C:\demos\LoggingGeneratorFormatErrorExample\Class1.cs(9,75): warning SYSLIB1015: Argument 'person' is not referenced from the logging message [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]
C:\demos\LoggingGeneratorFormatErrorExample\Class1.cs(8,6): error SYSLIB1014: Template '@Person' is not provided as argument to the logging method [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]
C:\demos\LoggingGeneratorFormatErrorExample\Microsoft.Extensions.Logging.Generators\Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator\LoggerMessage.g.cs(20,41): error CS0119: 'Person' is a type, which is not valid in the given context [C:\demos\LoggingGeneratorFormatErrorExample\LoggingGeneratorErrorExample.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: dasMulli
Assignees: -
Labels:

untriaged, area-Extensions-Logging

Milestone: -

@martincostello
Copy link
Member

martincostello commented Dec 9, 2021

Partial dupe of #60968 for usage of @ (I didn't encounter the $ case).

@ghost ghost locked as resolved and limited conversation to collaborators Jan 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Logging untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants