Is there any way to log something like the following in asp.net core 2?
_logger.LogInformation("Someone did something! \r\n{detail}", new {
SomeObject = someVariable,
SomeOtherObject = someOtherVariable
});
I've tried it with serialized values but the resulting json seems to get truncated.
The parameter you're attempting to use is for args, which are generally used for string interpolation of the message. Some logging providers, such as Serilog, will persist these args as JSON, but then you're relying on the particular provider and how they opt to handle it.
If you're goal is to simply have the {detail}
substituted with a JSON object, then just do that directly in the message:
var detail = JsonConvert.SerializeObject(new
{
SomeObject = someVariable,
SomeOtherObject = someOtherVariable
});
_logger.LogInformation($"Someone did something! \r\n{detail}");