使用 HTTP 模块处理应用程序事件并记录应用程序信息






3.36/5 (17投票s)
本文介绍如何创建一个 HTTP 模块,该模块在处理页面请求期间处理应用程序事件。
引言
本文介绍如何创建一个 HTTP 模块,该模块在处理页面请求期间处理应用程序事件。示例模块会将信息写入页面生命周期中各个点的日志文件。我们还将了解如何在示例网站中配置 HTTP 模块以供使用。
什么是 HTTP 模块?
HTTP 模块是一个托管组件,可以响应 ASP.NET 运行时引发的事件。响应应用程序事件的事件处理程序也可以位于 HTTP 模块中。使用 HTTP 模块的好处在于,它提供了一个“可插入”的组件,可以通过简单地将 HTTP 模块程序集放入 /bin 目录,并在应用程序的 Web.config 中添加一两行配置,就可以将其添加到现有的 ASP.NET 应用程序中。
构建 HTTP 模块
在此,我们使用 System.Web.IHttpModule
接口,其中定义了三个成员:必须实现的两个方法和一个只读属性。Init
方法接受一个 HttpApplication
对象,并返回 void
。Init
方法由 ASP.NET 调用,以使模块能够处理传入的请求。Dispose
方法不带参数,并返回 void
。Dispose
方法由 ASP.NET 调用,开发人员使用它来执行在 ASP.NET 运行时销毁对象之前的任何最终清理工作。ModuleName
属性应返回一个字符串,其中包含该模块的友好名称。
该示例将演示如何使用 Application 对象的 BeginRequest
和 EndRequest
事件。当引发 BeginRequest
和 EndRequest
事件时,我们的代码将简单地将信息写入日志文件。请记住,ASP.NET 的 HTTP 运行时并不限于只有一个模块参与请求;您可以轻松地让多个模块在传入和传出方向上对每个请求采取行动。正在请求的实际文件仍将返回给客户端。在这种情况下,返回的文件将在流的开头和结尾添加信息。此代码可以在 LogWriter\LogWriter\LogWriter\LogModule.cs 中找到。
(1) namespace LogWriter
(2) {
(3) public class LogModule : IHttpModule
(4) {
(5) System.IO.StreamWriter logWriter;
(6) HttpApplication applicationContext;
(7) EventHandler beginRequestEventHandler;
(8) EventHandler endRequestEventHandler;
(9) String logName;
(10) public void Init(HttpApplication context)
(11) {
(12) //Store file name for later use.
(13) logName = context.Context.Request.PhysicalApplicationPath + "log.txt";
(14) //Open file and write information.
(15) logWriter = new System.IO.StreamWriter(logName, true);
(16) logWriter.WriteLine("Date :" +
System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss"));
(17) logWriter.WriteLine("Event : Init");
(18) logWriter.Flush();
(19) logWriter.Close();
(20) //Store for access in other events.
(21) applicationContext = context;
(22) beginRequestEventHandler = new System.EventHandler(this.OnBeginRequest);
(23) applicationContext.BeginRequest += beginRequestEventHandler;
(24) endRequestEventHandler = new System.EventHandler(this.OnEndRequest);
(25) applicationContext.BeginRequest += endRequestEventHandler;
(26) }
(27) public void Dispose()
(28) {
(29) //Open file and write information.
(30) logWriter = new System.IO.StreamWriter(logName, true);
(31) logWriter.WriteLine("");
(32) logWriter.WriteLine("Date :" +
System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss"));
(33) logWriter.WriteLine("Event : Dispose");
(34) logWriter.WriteLine("");
(35) logWriter.Flush();
(36) logWriter.Close();
(37) //Clean-up.
(38) applicationContext.BeginRequest -= beginRequestEventHandler;
(39) beginRequestEventHandler = null;
(40) applicationContext.EndRequest -= beginRequestEventHandler;
(41) beginRequestEventHandler = null;
(42) }
(43) protected void OnBeginRequest(object sender, EventArgs e)
(44) {
(45) //Open file and write log information.
(46) logWriter = new System.IO.StreamWriter(logName, true);
(47) logWriter.WriteLine("");
(48) logWriter.WriteLine("Date :" +
System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss"));
(49) logWriter.WriteLine("Event : Application_BeginRequest ");
(50) logWriter.WriteLine("UserAgent : " +
applicationContext.Context.Request.UserAgent);
(51) logWriter.WriteLine("HostAddress : " +
applicationContext.Context.Request.UserHostAddress);
(52) logWriter.WriteLine("HostName : " +
applicationContext.Context.Request.UserHostName);
(53) logWriter.WriteLine("SiteURL : " +
applicationContext.Context.Request.Url.ToString());
(54) logWriter.WriteLine("");
(55) logWriter.Flush();
(56) logWriter.Close();
(57) }
(58) protected void OnEndRequest(object sender, EventArgs e)
(59) {
(60) //Open file and write log information.
(61) logWriter = new System.IO.StreamWriter(logName, true);
(62) logWriter.WriteLine("");
(63) logWriter.WriteLine("Date :" +
System.DateTime.Now.ToString("MM.dd.yyyy HH:mm:ss"));
(64) logWriter.WriteLine("Event : Application_EndRequest ");
(65) logWriter.WriteLine("UserAgent : " +
applicationContext.Context.Request.UserAgent);
(66) logWriter.WriteLine("HostAddress : " +
applicationContext.Context.Request.UserHostAddress);
(67) logWriter.WriteLine("HostName : " +
applicationContext.Context.Request.UserHostName);
(68) logWriter.WriteLine("SiteURL : " +
applicationContext.Context.Request.Url.ToString());
(69) logWriter.WriteLine("");
(70) logWriter.Flush();
(71) logWriter.Close();
(72) }
(73) }
(74) }
代码的演练表明,在第 3 行,我们正在创建一个名为 LogModule
的类,该类实现了 IHttpModule
接口。第 10 行的 Init
方法和第 27 行的 Dispose
方法都是 IHttpModule
接口所必需的。Init
方法注册两个 Application 事件处理程序:BeginRequest
和 EndRequest
。在这里,我们只是通知 ASP.NET HTTP 运行时,我们希望收到 application.BeginRequest
和 application.EndRequest
事件的通知。
如何配置 HTTP 模块
我们需要编辑应用程序的 web.config 文件以包含我们的新 IHttpModule
。Web.config 文件应如下所示
<configuration>
<system.web>
<httpModules>
<add type="LogWriter.LogModule" name="LogWriteModule"/>
</httpModules>
</system.web>
</configuration>
结论
上面的文章基本上是 HTTP 处理程序和 HTTP 模块的介绍,其中有一个简单的示例来演示如何使用 HTTP 模块处理应用程序事件。使用 HTTP 处理程序和 HTTP 模块可以使您的网站更易于管理、更具可扩展性,并且在演示层中编写的代码更少。您可以将所有逻辑封装在 HTTP 处理程序和 HTTP 模块中。暂时就这些。编程愉快!