如何记录ASP.NET Core输入JSON序列化错误 [英] How to log ASP.NET Core input JSON serialization errors

查看:79
本文介绍了如何记录ASP.NET Core输入JSON序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有ASP.NET Core应用程序,我们将其用作REST api.方法使用[FromBody]MyClass param作为输入.问题是,如果客户端发送无效的JSON字符串,由于JSON序列化错误,input值将变为null.有没有记录这种错误的方法?

We have ASP.NET Core application which we use as a REST api. Methods use [FromBody]MyClass param as input. Problem is if the client sends an invalid JSON string the input value becomes null due to JSON serialization errors. Is there a way to log such errors?

我正在寻找应用程序范围内的解决方案,而不是按方法进行..

I'm looking for application-wide solution not per-method..

推荐答案

我已通过在Startup.cs中添加错误处理程序解决了此问题:

I've solved this with adding an error handler in Startup.cs:

services.AddMvc()
        .AddJsonOptions(options => {
              options.SerializerSettings.Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) =>
              {
                    //Log args.ErrorContext.Error details...
              };
        });

这篇关于如何记录ASP.NET Core输入JSON序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆