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.