Skip to content

LoggerMessageAttribute generated source does not respect escaped characters and does not compile #56203

Closed

Description

@JakenVeina

Description

The source code generated for the following log method does not compile.

[LoggerMessage(
    EventId = 1,
    Level   = LogLevel.Debug,
    Message = "This is a log message: \"{Value1}\"")]
public static partial void SomeEvent(ILogger logger, string value1, string value2);

The fact that this log method includes parameters that are not encoded into Message forces a custom log state type to be generated, with the following code:

public override string ToString()
{
    var Value1 = this._value1;

    return $"This is a log message: "{Value1}"";
}

Configuration

Running .NET SDK 6.0.100-preview.6.21355.2 with version 6.0.0-preview.6.21352.12 of Microsoft.Extensions.Logging.Abstractions.

Activity

  1. ghost added
    untriaged
    on Jul 23, 2021
  2. added this to the 6.0.0 milestone on Jul 26, 2021
  3. removed
    untriaged
    on Jul 26, 2021
  4. maryamariyan commented on Jul 26, 2021

    @maryamariyan
    Contributor

    for your sample code, in the errors list you should see:

    warning SYSLIB1015: Argument 'value2' is not referenced from the logging message
    

    which points to what needs to get fixed.

  5. maryamariyan commented on Jul 26, 2021

    @maryamariyan
    Contributor

    Thanks for the report, here's another repro code:

    The code below works fine:

            [LoggerMessage(EventId = 1, Message = "This is a log message: {Value1} {Value2}")]
            public static partial void SomeEvent(ILogger logger, LogLevel logLevel, string value1, string value2);

    but for the snippet below with escaped characters, the generated code has issues:

            [LoggerMessage(EventId = 1, Message = "This is a log message: \"{Value1}\" \"{Value2}\"")]
            public static partial void SomeEvent(ILogger logger, LogLevel logLevel, string value1, string value2);
  6. ghost added
    in-pr
    on Aug 13, 2021
  7. self-assigned this
    on Aug 13, 2021
  8. ghost removed
    in-pr
    on Aug 14, 2021
  9. ghost locked as resolved and limited conversation to collaborators on Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions