Use IHttpModule for overall control on ASP. NET

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.