在 ASP.NET MVC & 中记录原始 HTTP 请求/响应IIS7 [英] Logging raw HTTP request/response in ASP.NET MVC & IIS7

查看:27
本文介绍了在 ASP.NET MVC & 中记录原始 HTTP 请求/响应IIS7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Web 服务(使用 ASP.NET MVC),出于支持目的,我们希望能够以尽可能接近原始在线格式(即包括 HTTP 方法、路径、所有标头和正文)到数据库中.

I'm writing a web service (using ASP.NET MVC) and for support purposes we'd like to be able to log the requests and response in as close as possible to the raw, on-the-wire format (i.e including HTTP method, path, all headers, and the body) into a database.

我不确定的是如何以最少损坏"的方式获取这些数据.我可以通过检查 HttpRequest 对象的所有属性并从它们构建一个字符串(对于响应类似)来重新构建我认为请求的样子,但我真的很想抓住在线发送的实际请求/响应数据.

What I'm not sure of is how to get hold of this data in the least 'mangled' way. I can re-constitute what I believe the request looks like by inspecting all the properties of the HttpRequest object and building a string from them (and similarly for the response) but I'd really like to get hold of the actual request/response data that's sent on the wire.

我很乐意使用任何拦截机制,例如过滤器、模块等,并且解决方案可以特定于 IIS7.但是,我更愿意将其保留在托管代码中.

I'm happy to use any interception mechanism such as filters, modules, etc. and the solution can be specific to IIS7. However, I'd prefer to keep it in managed code only.

有什么建议吗?

我注意到 HttpRequest 有一个 SaveAs 方法,该方法可以将请求保存到磁盘,但这会使用无法加载的内部辅助方法从内部状态重建请求可以公开访问(这就是为什么这不允许保存到我不知道的用户提供的流).所以看起来我必须尽我所能从对象中重建请求/响应文本......呻吟.

I note that HttpRequest has a SaveAs method which can save the request to disk but this reconstructs the request from the internal state using a load of internal helper methods that cannot be accessed publicly (quite why this doesn't allow saving to a user-provided stream I don't know). So it's starting to look like I'll have to do my best to reconstruct the request/response text from the objects... groan.

编辑 2:请注意,我说的是整个请求,包括方法、路径、标头等.当前的响应仅查看不包括此内容的正文流信息.

Edit 2: Please note that I said the whole request including method, path, headers etc. The current responses only look at the body streams which does not include this information.

编辑 3: 没有人阅读这里的问题吗?到目前为止有五个答案,但没有一个甚至暗示一种获得整个原始在线请求的方法.是的,我知道我可以从请求对象中捕获输出流、标头和 URL 以及所有这些内容.我已经在问题中说过了,请参阅:

Edit 3: Does nobody read questions around here? Five answers so far and yet not one even hints at a way to get the whole raw on-the-wire request. Yes, I know I can capture the output streams and the headers and the URL and all that stuff from the request object. I already said that in the question, see:

我可以通过检查 HttpRequest 对象的所有属性并从它们构建一个字符串(以及类似的响应)来重新构建我认为请求的样子,但我真的很想掌握实际的请求/response 在线发送的数据.

I can re-constitute what I believe the request looks like by inspecting all the properties of the HttpRequest object and building a string from them (and similarly for the response) but I'd really like to get hold of the actual request/response data that's sent on the wire.

如果您知道 完整 原始数据(包括标题、url、http 方法等)根本无法检索,那么了解这些将很有用.同样,如果您知道如何以原始格式获取所有内容(是的,我仍然是指包括标头、url、http 方法等)而不必重建它,这就是我所问的,那么这将非常有用.但是告诉我我可以从 HttpRequest/HttpResponse 对象重建它是没有用的.我知道.我已经说过了.

If you know the complete raw data (including headers, url, http method, etc.) simply cannot be retrieved then that would be useful to know. Similarly if you know how to get it all in the raw format (yes, I still mean including headers, url, http method, etc.) without having to reconstruct it, which is what I asked, then that would be very useful. But telling me that I can reconstruct it from the HttpRequest/HttpResponse objects is not useful. I know that. I already said it.

请注意:在有人开始说这是一个坏主意或会限制可扩展性等之前,我们还将在分布式环境中实施节流、顺序交付和抗重播机制,因此数据库无论如何都需要日志记录.我不是在寻找关于这是否是一个好主意的讨论,我正在寻找如何做到这一点.

推荐答案

好的,所以看起来答案是不,您无法获取原始数据,您必须根据解析的对象".哦,我已经完成了重建工作.

OK, so it looks like the answer is "no you can't get the raw data, you have to reconstruct the request/response from the properties of the parsed objects". Oh well, I've done the reconstruction thing.

这篇关于在 ASP.NET MVC & 中记录原始 HTTP 请求/响应IIS7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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