Elmah: How to get JSON HTTP request body from error report

后端 未结 3 1093
耶瑟儿~
耶瑟儿~ 2020-12-01 23:45

I\'m using Elmah to log exceptions. Elmah is great at logging request bodies if the request is a Form-based request (i.e. Content-Type: application/x-www-form-urlencoded),

3条回答
  •  天涯浪人
    2020-12-02 00:28

    first install nuget package : Newtonsoft.Json

    install-package Newtonsoft.Json
    

    then:

     public override void OnException(HttpActionExecutedContext filterContext)
            {
        var message = new StringBuilder();
                foreach (var param in filterContext.ActionContext.ActionArguments)
                {
                    message.Append(string.Format("{0}:{1}\r\n", param.Key, Newtonsoft.Json.JsonConvert.SerializeObject(param.Value)));
                }
                var ex = new Exception(message.ToString(), filterContext.Exception);
                var context = HttpContext.Current;
                ErrorLog.GetDefault(context).Log(new Error(ex, context));
    }
    

提交回复
热议问题