Julian Birch
Julian Birch

Reputation: 2758

Using a custom value text formatter with Microsoft ILogger

I've currently got some code that looks like this:

_logger.LogInformation("Found a problem {Problem}", Format(problemDetails));

where Format converts ProblemDetails to a string.

I would like to register a handler within the logging framework to explain how this type should be serialized to string so that I could type

_logger.LogInformation("Found a problem {Problem}", problemDetails);

and not just get the result of calling .ToString(). Is this possible?

Upvotes: 0

Views: 284

Answers (1)

Hossein Sabziani
Hossein Sabziani

Reputation: 3495

Just add an @ before the Problem. The problemDetails is written as Json:Structured logging

_logger.LogInformation("Found a problem {@Problem}", problemDetails);

Upvotes: 0

Related Questions