Skip to content

Logging Source Generator causes errors and warnings for messages using formatting specifiers #62572

Closed

Description

@dasMulli

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

Activity

  1. ghost added
    untriaged
    on Dec 9, 2021
  2. martincostello commented on Dec 9, 2021

    @martincostello
    Member

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

  3. ghost locked as resolved and limited conversation to collaborators on Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Logginguntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions