Using a custom value text formatter with Microsoft ILogger

159 Views Asked by At

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?

1

There are 1 best solutions below

1
Hossein Sabziani On

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

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