dotnet / dotnet/extensions

Introduce `ILogFormattable`

Open
#7,655 6 comments 4 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

Currently LOGGEN036 requires that `[LoggerMessage]` parameter types "implement `ToString()`, `IConvertible`, `IFormattable`".
There's a problem with this:
All of these interface + `ToString()` are general; if I override `ToString()` for my custom type for the purpose of modifying how it ends up appearing in log messages, I'll incidentally be changing how it's included in **_any_** string.

Imagine a scenario in which I'd want only the last 12 characters in a given instance of a custom identifier struct to appear in log messages (but in full everywhere else).

### API Proposal

Introduce `Microsoft.Extensions.Logging.Abstractions.ILogFormattable`:

### API Usage

```csharp
[ValueObject] // using the Vogen library
public readonly partial struct MyId : ILogFormattable
{
public string FormatForLog() => Value.ToString()[^12..];
}
```

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