Skip to content

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

@dasMulli

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions