In ASP. NET, if you want to record the number of requests currently and want to control the processing logic of each Exception, there are several methods. The first is to customize them on individual aspx pages, or extract the aspx page from a common Base Class. The second method is to modify the Global. asax file and write the Error processing logic in the Application_Error method. The third is to insert the implementation class of IHttpModule into the ASP. NET application in the form of a plug-in like. Let's take a look at the implementation of the third method.
First, write your IHttpModule subclass.
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 modules summary zookeeper
/// </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;
}
}
}
}
Then configure it in web. config.
<System. web>
<HttpModules>
<Add type = "Xudl. Test. TestModule" name = "TestModule"/>
</HttpModules>
</System. web>
After adding this information, run your web application and you will find that when an Exception is thrown on your page, the error information will be automatically recorded in your file. In addition, you need to slightly increase the permissions of your log files so that ASP. NET users have the permission to modify files. Otherwise, there is no way to append files.