extensions icon indicating copy to clipboard operation
extensions copied to clipboard

Introduce `ILogFormattable`

Open aradalvand opened this issue 7 months ago • 2 comments

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

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

Alternative Designs

No response

Risks

No response

aradalvand avatar Jun 01 '25 21:06 aradalvand