C# net的Web请求监视 问题背景
我有一些使用Microsoft.net framework 4.0开发并运行在IIS 7.0上的web应用程序。因此,现在我需要跟踪所有这些应用程序的流量信息(请求的用户、ip、应用程序名称、地址、请求时间等),并将其保存到数据库或文本文件中 当我搜索这个时,我发现了HTTP处理程序的概念 用我的箱子方便吗?或者是否有任何替代方案可以满足我的要求 我需要以plug abbe one的形式编写此组件。因为我还需要将此组件轻松连接到另一个web应用程序。C# net的Web请求监视 问题背景,c#,asp.net,visual-studio,iis-7,monitoring,C#,Asp.net,Visual Studio,Iis 7,Monitoring,我有一些使用Microsoft.net framework 4.0开发并运行在IIS 7.0上的web应用程序。因此,现在我需要跟踪所有这些应用程序的流量信息(请求的用户、ip、应用程序名称、地址、请求时间等),并将其保存到数据库或文本文件中 当我搜索这个时,我发现了HTTP处理程序的概念 用我的箱子方便吗?或者是否有任何替代方案可以满足我的要求 我需要以plug abbe one的形式编写此组件。因为我还需要将此组件轻松连接到另一个web应用程序。 欣赏您的想法使用HttpModule通过Be
欣赏您的想法使用
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()
{
}
}
使用
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模块,以覆盖所有基础。但一般来说,这应该适用于所有应用程序。谢谢,我将这样说:):首先感谢您的解决方案。顺便说一下,我还有一个问题。我是否可以将此组件插入任何asp.net web应用程序,而不更改web应用程序的代码:)@Renushi-是的,只要检索数据的机制相同,也就是说,如果检索登录用户的逻辑在应用程序之间不同,那么您可能需要为每个变体或排列构建一个单独的HTTP模块,以覆盖所有基础。但一般来说,这应该适用于所有应用程序。谢谢,我将这样说:)