ELMAH:如何从错误报告得到JSON HTTP请求正文错误报告、正文、ELMAH、HTTP

2023-09-02 10:51:14 作者:爱到极致心易碎、

我使用ELMAH来记录异常。 ELMAH是伟大的日志请求体,如果请求是一个基于表单的请求(即内容类型:应用程序/ x-WWW的形式urlen codeD),但基于JSON请求,其中内容类型是application / JSON ,请求主体是无处可在错误报告中。任何人都知道在哪里可以找到这个要求的身体,这样我可以正确诊断我的异常?

更新:2012-01-03

由于对我所说的基于JSON请求澄清,这里是使用JSON的例子原始的HTTP请求的请求体:

  PUT http://mycompany.com/api/v1.0/me HTTP / 1.1
主持人:mycompany.com
内容长度:20
内容类型:应用程序/ JSON

{城市:温哥华}
 

解决方案

ELMAH至今只记录上下文或信息是外围的请求,并可以方便地捕获在一个标准的方式。形式可以说是一种特殊的治疗,因为ASP.NET已经做解码的工作,的记忆的请求的实体时,MIME类型是应用程序/ x-WWW的形式urlen codeD 。另一方面JSON请求prolematic因为在发生异常时,输入流(Htt$p$pquest.InputStream)可能已被一个JSON德codeR部分或全部消耗掉。ELMAH将无法获得第二裂缝它记录的目的。因此,你必须确保你之前缓冲输入流或文本使其通过任何JSON德codeR和藏匿它什么地方像HttpContext.Items.然后,您可以尝试恢复缓冲数据和attach它的外发邮件的连接错误的时间。ELMAH目前不支持附加任意数据到记录的错误。然而,有在 ErrorLogModule ,有一个已记录事件并提供了记录错误的ID。这可以,如果你使用的后端数据库中使用存储在其他地方的输入数据(可能在另一个表错误日志),但通过标识保持关联比分扳成回记录的错误。

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), but with JSON based requests where the content type is application/json, the request body is nowhere to be found in the error reports. Anyone know where I can find this request body so that I can properly diagnose my exceptions?

浅谈浏览器 http 的缓存机制

UPDATE: 2012-01-03

As a clarification on what I mean by JSON based requests, here's an example raw HTTP request with JSON as the request body:

PUT http://mycompany.com/api/v1.0/me HTTP/1.1
Host: mycompany.com
Content-Length: 20
Content-Type: application/json

{"city":"Vancouver"}

解决方案

ELMAH so far only logs the context or information that is peripheral to the request and which can be conveniently captured in a standard way. Forms are arguably a special treatment because ASP.NET already does the job of decoding and memorizing request entities when the MIME type is application/x-www-form-urlencoded. JSON requests on the other hand are prolematic because at the time an exception occurs, the input stream (HttpRequest.InputStream) may have been partially or completely consumed by a JSON decoder. ELMAH would not be able to get a second crack at it for the purpose of logging. You will therefore have to make sure that you buffer the input stream or text before passing it through any JSON decoder and stash it away somewhere like HttpContext.Items. You could then try to recover the buffered data and attach it to an outgoing mail at the time of en error. ELMAH currently does not support attaching arbitrary data to a logged error. There is however the ErrorLogModule that has a Logged event and which supplies the Id of the logged error. This could be used store the input data elsewhere (perhaps in another table if you are using a back-end database for the error logs) but tie it back to the logged error by maintaining an association via the Id.