dotnet / dotnet/extensions

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

Open
#7,467 0 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-telemetry untriaged
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### 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:

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

Generated code:

```csharp
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.