Skip to content

Logging Source Generator fails ungracefully with overloaded methods #61814

Closed

Description

@JakeYallop

Description

The LoggerMessage source generator fails when there are overloaded logging methods.

Reproduction Steps

public static partial class LoggerMessageGeneration
{
    [LoggerMessage(EventId = 1, EventName = "LogInt", Level = LogLevel.Debug, Message = "Test message {One}")]
    public static partial void Log1(ILogger logger, int one);

    [LoggerMessage(EventId = 2, EventName = "LogBool", Level = LogLevel.Debug, Message = "Test message {Two}")]
    public static partial void Log1(ILogger logger, bool two);
}

Expected behavior

The code compiles successfully. If the LoggerMessage generator explicitly does not support logging method overloads, then a diagnostic should be emitted instead.

Consider the following code, where an overload is specified without an EventName - should this compile?

public static partial class LoggerMessageGeneration
{
    [LoggerMessage(EventId = 1, Level = LogLevel.Debug, Message = "Test message {One}")]
    public static partial void Log1(ILogger logger, int one);

    [LoggerMessage(EventId = 2, Level = LogLevel.Debug, Message = "Test message {Two}")]
    public static partial void Log1(ILogger logger, bool two);
}

The current behaviour of the source generator is to use the method name as the event name, this would emit logs with different event ids that share the same event name. If there are logging providers that rely on event names being unique, this could be problematic.

Actual behavior

Emits

LoggerMessage.g.cs(19,152,19,166): error CS0102: The type 'LoggerMessageGeneration' already contains a definition for '__Log1Callback".

Regression?

No

Known Workarounds

Do not use overloaded methods when using [LoggerMessage].

Configuration

> dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.100
 Commit:    9e8b04bbff

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100\

Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 18, 2021
  2. removed
    untriaged
    on Dec 1, 2021
  3. added this to the 7.0.0 milestone on Dec 1, 2021
  4. self-assigned this
    on Jan 18, 2022
  5. JakeYallop commented on Jan 31, 2022

    @JakeYallop
    ContributorAuthor

    I'd love to take a look at providing a fix for this.

  6. ghost added
    in-pr
    on Jan 31, 2022
  7. maryamariyan commented on Feb 1, 2022

    @maryamariyan
    Contributor

    @JakeYallop thanks for your interest. I have a fix prepared already. It also is addressing some other source generator feedback so we would end up having to keep either one of the two. I reviewed your PR in the meantime.

  8. added a commit that references this issue on Feb 2, 2022
    2e35741
  9. ghost removed
    in-pr
    on Feb 2, 2022
  10. ghost locked as resolved and limited conversation to collaborators on Mar 4, 2022
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