在ASP.NET中,如果想记录一下当前有多少个Request,想控制每个Exception的处理逻辑,有好几种办法,第一种就是在个别的aspx页面中定制,或者把aspx页面提取一个共通的Base Class。第二种方法就是修改Global.asax文件,在其中的Application_Error方法中写入Error处理逻辑。第三种就是采用类似插件的形式,把IHttpModule的实现类插入进ASP.NET应用中。我们来说一下第三种方法的实现。
首先,编写你的IHttpModule子类。
using
System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Xudl.Test
... {
/**//// <summary>
/// TestModule の概要の説明です
/// </summary>
public class TestModule : IHttpModule
...{
public TestModule()
...{
//
// TODO: コンストラクタ ロジックをここに追加します
//
}
private HttpApplication app = null;
public void Init(HttpApplication application)
...{
application.EndRequest +=
(new EventHandler(this.Application_EndRequest));
application.Error +=
(new EventHandler(this.Application_Error));
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source,
EventArgs e)
...{
System.IO.File.AppendAllText(@"d:/test.log", "begin ");
}
private void Application_EndRequest(Object source, EventArgs e)
...{
HttpApplication app = source as HttpApplication;
System.IO.File.AppendAllText(@"d:/test.log", "end ");
}
private void Application_Error(Object source, EventArgs e)
...{
System.IO.File.AppendAllText(@"d:/test.log", "error ");
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo = "<br>Offending URL: " + ctx.Request.Url.ToString() +
"<br>Source: " + exception.Source +
"<br>Message: " + exception.Message +
"<br>Stack trace: " + exception.StackTrace;
ctx.Response.Write(errorInfo);
ctx.Server.ClearError ();
}
public void Dispose()
...{
if (null != app)
...{
app.BeginRequest -= new EventHandler(Application_BeginRequest);
app.EndRequest -= new EventHandler(Application_EndRequest);
app.Error -= new EventHandler(Application_Error);
app = null;
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Xudl.Test
... {
/**//// <summary>
/// TestModule の概要の説明です
/// </summary>
public class TestModule : IHttpModule
...{
public TestModule()
...{
//
// TODO: コンストラクタ ロジックをここに追加します
//
}
private HttpApplication app = null;
public void Init(HttpApplication application)
...{
application.EndRequest +=
(new EventHandler(this.Application_EndRequest));
application.Error +=
(new EventHandler(this.Application_Error));
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source,
EventArgs e)
...{
System.IO.File.AppendAllText(@"d:/test.log", "begin ");
}
private void Application_EndRequest(Object source, EventArgs e)
...{
HttpApplication app = source as HttpApplication;
System.IO.File.AppendAllText(@"d:/test.log", "end ");
}
private void Application_Error(Object source, EventArgs e)
...{
System.IO.File.AppendAllText(@"d:/test.log", "error ");
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo = "<br>Offending URL: " + ctx.Request.Url.ToString() +
"<br>Source: " + exception.Source +
"<br>Message: " + exception.Message +
"<br>Stack trace: " + exception.StackTrace;
ctx.Response.Write(errorInfo);
ctx.Server.ClearError ();
}
public void Dispose()
...{
if (null != app)
...{
app.BeginRequest -= new EventHandler(Application_BeginRequest);
app.EndRequest -= new EventHandler(Application_EndRequest);
app.Error -= new EventHandler(Application_Error);
app = null;
}
}
}
}
然后再web.config中配置一下。
<
system
.web
>
< httpModules >
< add type ="Xudl.Test.TestModule" name ="TestModule" />
</ httpModules >
</ system.web >
< httpModules >
< add type ="Xudl.Test.TestModule" name ="TestModule" />
</ httpModules >
</ system.web >
添加了这些以后,你再运行一下你的web应用程序,就发现在你的页面抛出Exception的时候,错误信息就会自动记录到你的文件中去了。另外,你需要把你的log文件的权限稍微开高一点,让ASP.NET用户具有修改文件的权限,否则文件是没有办法进行追加操作的,这点需要注意。