I have an API that is called by an external application, and I believe we're having a lot of 400 errors.
I've looked up how to catch and log automatic HTTP 400 errors, and found this solution:
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
ILogger logger = context.HttpContext.RequestServices
.GetRequiredService<ILogger>();
string keys = JsonConvert.SerializeObject(context.ModelState.Keys);
string values = JsonConvert.SerializeObject(context.ModelState.Values);
logger.Debug($"Keys: {keys}, Values: {values}");
return new BadRequestObjectResult(context.ModelState);
};
});
This works well, but I assumed that I would get all keys and all values, when in fact, it only shows the keys that has an error, and not even their sent value.
Is there a way to log the whole request body in InvalidModelStateResponseFactory or am I looking at it in the wrong way?
Note that I don't want to use the SuppressModelStateInvalidFilter option.