I send and receive huge JSON Payloads to/from Webservices. I log JSON content at Debug level in easy to read format (Prettifier) and response Code at Information level.
_logger.LogInformation($"statusCode={responseMessage.StatusCode}");
_logger.LogDebug(MyJsonPrettifier(await responseMessage.Content.ReadAsStringAsync));
This is really useful during development - I can quickly inspect what's going on. However, due to how these functions are structured - simple string argument - it will always be executed even if I set the log level to different - Warning when Publish Release version.
I read this article where the difference is shown - application without logs performs 40x faster. The problem is really serious. I develop in .NET5 and I also use Serilog. Is there an easy, universal, perhaps established way to approach this problem?
I could easily improve the problem with a logger that takes callbacks as arguments. Therefore it would only execute the callback if the appropriate Log Level is set.
_logger.LogInformation(
() => $"statusCode={responseMessage.StatusCode}");
_logger.LogDebug(
() => MyJsonPrettifier(await responseMessage.Content.ReadAsStringAsync)
);