2010-08-19 53 views
22

我需要爲發送到IIS的請求記錄請求發佈負載。是否可以使用IIS 7.5中的現有日誌記錄和高級日誌記錄模塊來配置請求後有效負載的日誌記錄,或者任何人都可以將我指向任何允許我記錄後期負載的自定義模塊。IIS 7日誌請求正文

回答

13

它實際上可以做,根據https://serverfault.com/a/90965,具有諷刺意味的是回答這個大衛·席爾瓦·史密斯掛在他的答案在這裏,聲稱它不能做的問題;)

的IIS日誌只記錄查詢字符串和標題信息,不包含任何POST數據。

如果您使用IIS7,則可以爲 狀態碼200啓用失敗請求跟蹤。這將記錄所有數據,並且您可以選擇要包含的數據類型的 。

+1

你看不到請求體這樣反正...只有頭和一些診斷信息。 – Azimuth 2013-12-13 09:49:50

+4

@Azimuth我沒有證實這一點,但顯然這些字段分別包含請求和響應的http正文:'GENERAL_REQUEST_ENTITY'和'GENERAL_RESPONSE_ENTITY_BUFFER' – 2013-12-16 01:16:48

+5

@Azimuth不正確。我設置了失敗請求追蹤並收回整個POST正文。幫助我找出提交給我的API的具體內容:http://blogs.msdn.com/b/benjaminperkins/archive/2012/01/02/enable-and-activate-failed-request-tracing-rules.aspx ?重定向=真 – 2014-05-14 13:02:00

4

我成功地創建了我的請求包含整個請求(頭和響應),我只用它來記錄特定崗位要求的文本文件:

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

希望這可以幫助你!

0

這裏是我們用來記錄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中註冊它。

使用system.WebServer節IIS集成模型

<system.webServer> 
    <modules> 
      <add name="HttpPOSTLogger" type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules" /> 
    </modules> 
</system.webServer> 

的IIS經典模式

<system.web> 
    <httpModules> 
   <add name="HttpPOSTLogger" type="MySolution.HttpModules.HttpPOSTLogger, MySolution.HttpModules"/> 
  </httpModules> 
</system.web> 

IIS日誌使用的System.Web部分應用模塊:

::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