【问题标题】:IIS 7 Log Request BodyIIS 7 日志请求正文
【发布时间】:2010-08-19 14:57:28
【问题描述】:

我需要为向 IIS 发出的请求记录请求后有效负载。这是否可以使用 IIS 7.5 中的现有日志记录和高级日志记录模块配置请求发布有效负载的日志记录,或者任何人都可以将我引导到任何允许我记录发布有效负载的自定义模块。

【问题讨论】:

    标签: logging iis-7 request


    【解决方案1】:

    其实是可以做到的,根据https://serverfault.com/a/90965

    IIS 日志只记录查询字符串和标头信息,没有 任何 POST 数据。

    如果您使用的是 IIS7,您可以为 状态码 200。这将记录所有数据,您可以选择 要包含哪种类型的数据。

    【讨论】:

    • 无论如何你都看不到请求正文...只有标头和一些诊断信息。
    • @Azimuth 我还没有验证这一点,但显然这些字段分别包含请求和响应的 http 正文:GENERAL_REQUEST_ENTITYGENERAL_RESPONSE_ENTITY_BUFFER
    • @Azimuth 不正确。我设置了失败的请求跟踪并取回了整个 POST 正文。帮助我弄清楚提交给我的 API 的确切内容:blogs.msdn.com/b/benjaminperkins/archive/2012/01/02/…
    • 看起来只有在实际存在请求正文时才能看到请求正文。 GENERAL_REQUEST_ENTITY 正在为我展示它(位于 GENERAL_REQUEST_HEADERS 下方,大概会一直在那里)。
    【解决方案2】:

    我设法为我的请求创建了一个包含整个请求(标头和响应)的文本文件,我只用它来记录特定的发布请求:

    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);
    }
    

    希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      这是我们用来记录 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-09
        • 2011-10-18
        • 2016-08-19
        • 1970-01-01
        • 2019-06-05
        相关资源
        最近更新 更多