Capture exceptions in ASP. NET programs

Source: Internet
Author: User

Catch ASP. NET exceptions. This article is not uncommon on the Internet. Before that, I read a lot of code written by others and learned a lot from others. On the basis of others, I have added some self-written items. Now I am posting them and sharing them. I hope it will be helpful to you.

1 using system. text;
2 using system. Web;
3 using system. configuration;
4 using system. IO;
5
6 namespace hnrinfo. Framework. httpmodule
7 {
8/** // <summary>
9 // capture unprocessed system errors, add errors to the database, and point to a friendly error page
10 /// </Summary>
11 public class catcherror: ihttpmodule
12 {
13 Private Static string syserrorpage = configurationmanager. receivettings ["syserrorpage"]. tostring ();
14
15 public void dispose ()
16 {
17}
18
19 public void Init (httpapplication context)
20 {
21 context. Error + = new eventhandler (saveerror );
22}
23 // Insert the captured error information into the database
24 private void saveerror (Object sender, eventargs E)
25 {
26 httpcontext context = (httpapplication) sender). context;
27 string Path = context. Request. rawurl;
28 string hostip = context. Request. userhostaddress;
29
30 String title = string. empty;
31 string info = string. empty;
32
33 getlasterror (context. server. getlasterror (), ref title, ref info );
34
35 hnrinfo. model. systemerror errormodel = new hnrinfo. model. systemerror ();
36 errormodel. errortitle = title;
37 errormodel. errorinfo = Info;
38 errormodel. occururl = path;
39 errormodel. hostip = hostip;
40
41 hnrinfo. dalfactory. platfactory. systemerror. Add (errormodel );
42
43 // redirect to a friendly error page
44 context. server. Transfer (syserrorpage, false );
45}
46
47/*** // <summary>
48 /// locate the initial error that causes the exception
49 // </Summary>
50 private void getlasterror (exception E, ref String title, ref string info)
51 {
52 If (E. innerexception! = NULL)
53 getlasterror (E. innerexception, ref title, ref info );
54 else
55 {
56 Title = E. message;
57 info = E. stacktrace;
58}
59}
60}
61}
62

Lines 35-41 are some operations that are stored in the database. Ignore these lines of code. The configuration of this Code in Web. config is as follows:

<Httpmodules>
<! -- Capture error -->
<Add type = "hnrinfo. Framework. httpmodule. catcherror, hnrinfo. Framework" name = "hnrinfo. Framework"/>
</Httpmodules>

There is nothing in the code, which is quite simple because. Net has done a lot of complicated work for us in the background. I think if you have time, let's take a look at the usage of this stacktrace type. Below I will list some of my friends' articles (about stacktrace usage ):
Analysis of stacktrace
Use stacktrace to obtain more exception-related information
Use System. diagnostices. stacktrace to obtain the call heap metadata.

Related Article

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.