【问题标题】:Web request monitoring for .net.net 的 Web 请求监控
【发布时间】:2013-11-27 04:40:54
【问题描述】:

问题背景

我很少有使用 Microsoft .net 框架 4.0 开发并在 IIS 7.0 上运行的 Web 应用程序。所以现在我需要跟踪流量信息(请求的用户、IP、应用程序名称、地址、请求时间等)所有这些应用程序并持久化到数据库或文本文件等中。

当我搜索这个时,我发现了 HTTP 处理程序的概念。

用我的箱子方便吗?或者有什么替代方案可以满足我的要求?

我需要将此组件编写为plug-abbe one。因为我还需要将此组件轻松连接到另一个Web应用程序。

欣赏你的想法

【问题讨论】:

    标签: c# asp.net visual-studio iis-7 monitoring


    【解决方案1】:

    使用HttpModule 通过BeginRequest 事件拦截所有请求,如下所示:

    public class RequestLoggingModule : IHttpModule
    {
        private HttpApplication httpApp;
    
        public void Init(HttpApplication httpApp)
        {
            this.httpApp = httpApp;
            httpApp.BeginRequest += new EventHandler(OnBeginRequest);
        }
    
        void OnBeginRequest(object sender, EventArgs e)
        {
            // Get the user that made the request
            HttpApplication application = (HttpApplication)sender;
            HttpResponse response = application.Context.Response;
    
            WindowsIdentity identity = 
               (WindowsIdentity)application.Context.User.Identity;
    
            LogInformation(identity.Name);
    
            // Do this for other information you want to log here
    
        }
    
        private void LogInformation(string data)
        {
            EventLog log = new EventLog();
            log.Source = "Application XYZ Request Logging";
            log.WriteEntry(data, EventLogEntryType.Information);
        }
    
        public void Dispose()
        {
    
        }
    }
    

    【讨论】:

    • :首先感谢您的解决方案。顺便说一句,我还有一个问题。我可以将此组件插入任何 asp .net Web 应用程序,而无需对 Web 应用程序进行代码更改。 :)
    • @Renushi - 是的,只要检索数据的机制相同,即如果检索登录用户的逻辑因应用程序而异,那么您可能需要构建一个单独的 HTTP 模块对于每个变体或排列以覆盖所有碱基。但一般来说,这应该适用于跨应用程序。
    • 谢谢,我会这样:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2012-01-16
    • 2010-12-21
    相关资源
    最近更新 更多