【发布时间】:2010-08-19 14:57:28
【问题描述】:
我需要为向 IIS 发出的请求记录请求后有效负载。这是否可以使用 IIS 7.5 中的现有日志记录和高级日志记录模块配置请求发布有效负载的日志记录,或者任何人都可以将我引导到任何允许我记录发布有效负载的自定义模块。
【问题讨论】:
我需要为向 IIS 发出的请求记录请求后有效负载。这是否可以使用 IIS 7.5 中的现有日志记录和高级日志记录模块配置请求发布有效负载的日志记录,或者任何人都可以将我引导到任何允许我记录发布有效负载的自定义模块。
【问题讨论】:
其实是可以做到的,根据https://serverfault.com/a/90965
IIS 日志只记录查询字符串和标头信息,没有 任何 POST 数据。
如果您使用的是 IIS7,您可以为 状态码 200。这将记录所有数据,您可以选择 要包含哪种类型的数据。
【讨论】:
GENERAL_REQUEST_ENTITY 和 GENERAL_RESPONSE_ENTITY_BUFFER
我设法为我的请求创建了一个包含整个请求(标头和响应)的文本文件,我只用它来记录特定的发布请求:
protected void Application_BeginRequest(Object Sender, EventArgs e)
{
string uniqueid = Guid.NewGuid().ToString();
string logfile = String.Format("C:\\path\\to\\folder\\requests\\{0}.txt", uniqueid);
Request.SaveAs(logfile, true);
}
希望对您有所帮助!
【讨论】:
这是我们用来记录 HTTP POST 请求数据的自定义 HTTP 模块的代码。
using System;
using System.Web;
namespace MySolution.HttpModules
{
public class HttpPOSTLogger : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
if (sender != null && sender is HttpApplication)
{
var request = (sender as HttpApplication).Request;
var response = (sender as HttpApplication).Response;
if (request != null && response != null && request.HttpMethod.ToUpper() == "POST")
{
var body = HttpUtility.UrlDecode(request.Form.ToString());
if (!string.IsNullOrWhiteSpace(body))
response.AppendToLog(body);
}
}
}
}
}
不要忘记在你的应用程序的 web.config 中注册它。
为 IIS 集成模型使用 system.WebServer 部分
<system.webServer>
<modules>
<add name="HttpPOSTLogger" type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules" />
</modules>
</system.webServer>
为 IIS 经典模型使用 system.web 部分
<system.web>
<httpModules>
<add name="HttpPOSTLogger" type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules"/>
</httpModules>
</system.web>
应用模块前的IIS日志:
::1, -, 10/31/2017, 10:53:20, W3SVC1, machine-name, ::1, 5, 681, 662, 200, 0, POST, /MySolution/MyService.svc/MyMethod, -,
IIS日志应用模块后:
::1, -, 10/31/2017, 10:53:20, W3SVC1, machine-name, ::1, 5, 681, 662, 200, 0, POST, /MySolution/MyService.svc/MyMethod, {"model":{"Platform":"Mobile","EntityID":"420003"}},
全文:
https://www.codeproject.com/Tips/1213108/HttpModule-for-logging-HTTP-POST-data-in-IIS-Log
【讨论】: