【问题标题】:How to log HTTP requests coming into IIS如何记录进入 IIS 的 HTTP 请求
【发布时间】:2017-10-31 23:50:05
【问题描述】:

我在我的开发机器上运行 IIS 5。我有一个运行在其上的 asp.net 3.5 Web 服务,我从同一服务器上运行的不同 Web 应用程序调用该服务。我的服务返回错误 500 内部服务器错误,我正在对其进行故障排除。我的请求是通过System.Net.HttpWebRequest 对象发送的,从客户的角度来看,它看起来是有效的。

我想从服务器的角度查看原始传入的 HTTP 请求。由于该服务是在环回上调用的,因此我无法使用 Wireshark 来查看它。

IIS 日志显示请求标头,但不显示帖子内容。

关于如何在 IIS 中查看完整的原始传入 HTTP 请求有什么建议吗?

谢谢

【问题讨论】:

    标签: http iis


    【解决方案1】:

    您是否尝试过使用Fiddler?只需使用您的机器名称而不是 localhost。

    【讨论】:

    • 如果您从浏览器查询,Fiddler 可以工作。从我自己的应用程序中调用 Web 服务将无法正常工作。
    • @Mr Greieves,这需要一些额外的工作,但您可以为您的网络服务调用手动配置代理设置。
    【解决方案2】:

    我认为您想添加一个 HTTPModule 用于日志记录。 Here's 一篇不错的关于 ASP.NET 模块和处理程序的文章:

    这样,当您想禁用日志记录时,您可以从您的 web.config 文件中删除/注释它。

    【讨论】:

      【解决方案3】:

      最好的办法是在不同的端口上运行每个 Web 应用程序,然后使用 Fiddler 之类的东西为要监视的端口创建代理。这将监控进出您的特定应用程序的所有流量。

      【讨论】:

        【解决方案4】:

        这是我们用来记录 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
          • 2015-10-12
          • 1970-01-01
          • 1970-01-01
          • 2021-11-27
          • 1970-01-01
          • 2014-05-13
          • 2019-03-09
          • 1970-01-01
          相关资源
          最近更新 更多