Skip to content

[API Proposal]: Optionally add CallerLineNumber and CallerFilePath to logging source generator #7467

@aelij

Description

@aelij

Background and motivation

When a generated logger method is called from multiple places, it can help debugging if we had the call site information. The compiler already knows how to emit those automatically using the CallerLineNumber and CallerFilePath attributes. By generating additional overloads tagged with OverloadResolutionPriority, we can easily get this information into each log call.

This can be an optional behavior enabled using an MSBuild property or an assembly/class attribute.

API Proposal

User code:

[EmitCallSiteInformation]
static partial class LoggerExtensions
{
    public static partial void LogMethod(ILogger logger);
}

Generated code:

static partial class LoggerExtensions

{
    public static partial void LogMethod(ILogger logger) => LogMethod(logger, default, default);
    
    [OverloadResolutionPriority(1)]
    public static void LogMethod(ILogger logger, [CallerFilePath] string? filePath = default, [CallerLineNumber] int lineNumber = default) ...
}

API Usage

See above

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions