要应用这个例子,需要在web.config/machine.config里添加IHttpModule的注册。详细看DotNet SDK里的httpModules的配置节。
当然appSettings也要注册链接字符串。
1
2 /**/ /*
3CREATE TABLE [Logs] (
4 [dt] [datetime] NOT NULL CONSTRAINT [DF_Logs_dt] DEFAULT (getdate()),
5 [SERVER_NAME] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
6 [UserHostAddress] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
7 [Path] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
8 [QueryString] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
9 [UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
10 [Category] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
11 [Message] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
12 CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED
13 (
14 [dt]
15 ) ON [PRIMARY]
16) ON [PRIMARY]
17GO
18
19CREATE TABLE [Exceptions] (
20 [dt] [datetime] NOT NULL CONSTRAINT [DF_Exceptions_dt] DEFAULT (getdate()),
21 [SERVER_NAME] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
22 [UserHostAddress] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
23 [Path] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
24 [QueryString] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
25 [UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
26 [Message] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
27 [ToString] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL ,
28 CONSTRAINT [PK_Exceptions] PRIMARY KEY CLUSTERED
29 (
30 [dt]
31 ) ON [PRIMARY]
32) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
33GO
34*/
35 namespace AspNetMonitor
36 {
37 using System;
38 using System.Collections;
39 using System.Collections.Specialized;
40 using System.Configuration;
41 using System.Data;
42 using System.Data.SqlClient;
43 using System.IO;
44 using System.Threading;
45 using System.Web;
46 using System.Text;
47
48 public class Module : IHttpModule
49 {
50 HttpApplication app;
51 DateTime dtstart=DateTime.Now;
52
53 System.Timers.Timer timer;
54
55
56 HttpContext Context
57 {
58 get
59 {
60 return app.Context;
61 }
62 }
63
64 public void Init(HttpApplication application)
65 {
66 if(app!=null)throw(new InvalidOperationException());
67
68 app=application;
69
70 app.PreRequestHandlerExecute+=new EventHandler(app_PreRequestHandlerExecute);
71 app.PostRequestHandlerExecute+=new EventHandler(app_PostRequestHandlerExecute);
72 app.Error+=new EventHandler(app_Error);
73 }
74 public void Dispose()
75 {
76 app.PreRequestHandlerExecute-=new EventHandler(app_PreRequestHandlerExecute);
77 app.PostRequestHandlerExecute-=new EventHandler(app_PostRequestHandlerExecute);
78 app.Error-=new EventHandler(app_Error);
79 app=null;
80 }
81
82 private void app_Error(object sender, EventArgs e)
83 {
84 if(timer!=null)
85 {
86 timer.Dispose();
87 timer=null;
88 }
89
90 Exception x=Context.Server.GetLastError();
91 while(x is HttpUnhandledException)
92 x=x.InnerException;
93 if(x==null)return;
94
95 NameValueCollection nvc=new NameValueCollection();
96
97 HttpRequest request=Context.Request;
98
99 nvc.Add("SERVER_NAME" ,request.ServerVariables["SERVER_NAME"]);
100 nvc.Add("UserHostAddress" ,request.UserHostAddress);
101 nvc.Add("Path" ,request.Path);
102 nvc.Add("QueryString" ,request.ServerVariables["QUERY_STRING"]);
103
104 nvc.Add("UserName",Context.User.Identity.Name);
105
106 nvc.Add("Message",x.Message);
107 nvc.Add("ToString",x.ToString());
108
109 InsertAsync(nvc,"Exceptions");
110 }
111
112
113 private void app_PreRequestHandlerExecute(object sender, EventArgs e)
114 {
115 if(timer!=null)
116 {
117 timer.Dispose();
118 timer=null;
119 }
120
121 dtstart=DateTime.Now;
122
123 timer=new System.Timers.Timer(10000);
124 timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
125 timer.AutoReset=true;
126 timer.Start();
127 }
128
129 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
130 {
131 if(timer!=null)
132 {
133 TimeSpan ts=DateTime.Now-dtstart;
134
135 NameValueCollection nvc=new NameValueCollection();
136
137 HttpRequest request=Context.Request;
138
139 nvc.Add("SERVER_NAME" ,request.ServerVariables["SERVER_NAME"]);
140 nvc.Add("UserHostAddress" ,request.UserHostAddress);
141 nvc.Add("Path" ,request.Path);
142 nvc.Add("QueryString" ,request.ServerVariables["QUERY_STRING"]);
143
144 nvc.Add("UserName",Context.User.Identity.Name);
145
146 nvc.Add("Category","Handler");
147
148 nvc.Add("Message",">"+ts.TotalSeconds.ToString());
149
150 InsertAsync(nvc,"Logs");
151 }
152 }
153
154 private void app_PostRequestHandlerExecute(object sender, EventArgs e)
155 {
156 if(timer!=null)
157 {
158 timer.Dispose();
159 timer=null;
160 }
161 }
162
163
164
165 static void InsertAsync(NameValueCollection nvc,string tablename)
166 {
167 new Thread(new ThreadStart(new Job(nvc,tablename).Start)).Start();
168 }
169
170 class Job
171 {
172 static void AddParameter(SqlCommand cmd,string pname,string value)
173 {
174 if(value==null)
175 {
176 value="";
177 }
178 if(value.Length<4000)
179 {
180 cmd.Parameters.Add("@"+pname,SqlDbType.NVarChar,4000).Value=value;
181 return;
182 }
183 cmd.Parameters.Add("@"+pname,SqlDbType.NText).Value=value;
184 }
185
186 static string _connstr;
187 static SqlConnection CreateConnection()
188 {
189 if(_connstr==null)_connstr=ConfigurationSettings.AppSettings["AspNetMonitorConnectionString"];
190 return new SqlConnection(_connstr);
191 }
192
193
194 NameValueCollection n;
195 string t;
196 public Job(NameValueCollection nvc,string tablename)
197 {
198 n=nvc;
199 t=tablename;
200 }
201 public void Start()
202 {
203 using(SqlConnection conn=CreateConnection())
204 {
205 using(SqlCommand cmd=conn.CreateCommand())
206 {
207 StringBuilder sb1=new StringBuilder();
208 StringBuilder sb2=new StringBuilder();
209 foreach(string key in n.Keys)
210 {
211 AddParameter(cmd,key,n[key]);
212 if(sb1.Length!=0)sb1.Append(",");
213 if(sb2.Length!=0)sb2.Append(",");
214 sb1.Append(key);
215 sb2.Append("@");
216 sb2.Append(key);
217 }
218
219 cmd.CommandText="INSERT INTO "+t+" ( "+sb1+" ) VALUES ( "+sb2+" ) ";
220
221 conn.Open();
222 cmd.ExecuteNonQuery();
223 conn.Close();
224 }
225 }
226 }
227 }
228
229
230 }
231}
2 /**/ /*
3CREATE TABLE [Logs] (
4 [dt] [datetime] NOT NULL CONSTRAINT [DF_Logs_dt] DEFAULT (getdate()),
5 [SERVER_NAME] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
6 [UserHostAddress] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
7 [Path] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
8 [QueryString] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
9 [UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
10 [Category] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
11 [Message] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
12 CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED
13 (
14 [dt]
15 ) ON [PRIMARY]
16) ON [PRIMARY]
17GO
18
19CREATE TABLE [Exceptions] (
20 [dt] [datetime] NOT NULL CONSTRAINT [DF_Exceptions_dt] DEFAULT (getdate()),
21 [SERVER_NAME] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
22 [UserHostAddress] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
23 [Path] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
24 [QueryString] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
25 [UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
26 [Message] [nvarchar] (500) COLLATE Chinese_PRC_CI_AS NOT NULL ,
27 [ToString] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL ,
28 CONSTRAINT [PK_Exceptions] PRIMARY KEY CLUSTERED
29 (
30 [dt]
31 ) ON [PRIMARY]
32) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
33GO
34*/
35 namespace AspNetMonitor
36 {
37 using System;
38 using System.Collections;
39 using System.Collections.Specialized;
40 using System.Configuration;
41 using System.Data;
42 using System.Data.SqlClient;
43 using System.IO;
44 using System.Threading;
45 using System.Web;
46 using System.Text;
47
48 public class Module : IHttpModule
49 {
50 HttpApplication app;
51 DateTime dtstart=DateTime.Now;
52
53 System.Timers.Timer timer;
54
55
56 HttpContext Context
57 {
58 get
59 {
60 return app.Context;
61 }
62 }
63
64 public void Init(HttpApplication application)
65 {
66 if(app!=null)throw(new InvalidOperationException());
67
68 app=application;
69
70 app.PreRequestHandlerExecute+=new EventHandler(app_PreRequestHandlerExecute);
71 app.PostRequestHandlerExecute+=new EventHandler(app_PostRequestHandlerExecute);
72 app.Error+=new EventHandler(app_Error);
73 }
74 public void Dispose()
75 {
76 app.PreRequestHandlerExecute-=new EventHandler(app_PreRequestHandlerExecute);
77 app.PostRequestHandlerExecute-=new EventHandler(app_PostRequestHandlerExecute);
78 app.Error-=new EventHandler(app_Error);
79 app=null;
80 }
81
82 private void app_Error(object sender, EventArgs e)
83 {
84 if(timer!=null)
85 {
86 timer.Dispose();
87 timer=null;
88 }
89
90 Exception x=Context.Server.GetLastError();
91 while(x is HttpUnhandledException)
92 x=x.InnerException;
93 if(x==null)return;
94
95 NameValueCollection nvc=new NameValueCollection();
96
97 HttpRequest request=Context.Request;
98
99 nvc.Add("SERVER_NAME" ,request.ServerVariables["SERVER_NAME"]);
100 nvc.Add("UserHostAddress" ,request.UserHostAddress);
101 nvc.Add("Path" ,request.Path);
102 nvc.Add("QueryString" ,request.ServerVariables["QUERY_STRING"]);
103
104 nvc.Add("UserName",Context.User.Identity.Name);
105
106 nvc.Add("Message",x.Message);
107 nvc.Add("ToString",x.ToString());
108
109 InsertAsync(nvc,"Exceptions");
110 }
111
112
113 private void app_PreRequestHandlerExecute(object sender, EventArgs e)
114 {
115 if(timer!=null)
116 {
117 timer.Dispose();
118 timer=null;
119 }
120
121 dtstart=DateTime.Now;
122
123 timer=new System.Timers.Timer(10000);
124 timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
125 timer.AutoReset=true;
126 timer.Start();
127 }
128
129 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
130 {
131 if(timer!=null)
132 {
133 TimeSpan ts=DateTime.Now-dtstart;
134
135 NameValueCollection nvc=new NameValueCollection();
136
137 HttpRequest request=Context.Request;
138
139 nvc.Add("SERVER_NAME" ,request.ServerVariables["SERVER_NAME"]);
140 nvc.Add("UserHostAddress" ,request.UserHostAddress);
141 nvc.Add("Path" ,request.Path);
142 nvc.Add("QueryString" ,request.ServerVariables["QUERY_STRING"]);
143
144 nvc.Add("UserName",Context.User.Identity.Name);
145
146 nvc.Add("Category","Handler");
147
148 nvc.Add("Message",">"+ts.TotalSeconds.ToString());
149
150 InsertAsync(nvc,"Logs");
151 }
152 }
153
154 private void app_PostRequestHandlerExecute(object sender, EventArgs e)
155 {
156 if(timer!=null)
157 {
158 timer.Dispose();
159 timer=null;
160 }
161 }
162
163
164
165 static void InsertAsync(NameValueCollection nvc,string tablename)
166 {
167 new Thread(new ThreadStart(new Job(nvc,tablename).Start)).Start();
168 }
169
170 class Job
171 {
172 static void AddParameter(SqlCommand cmd,string pname,string value)
173 {
174 if(value==null)
175 {
176 value="";
177 }
178 if(value.Length<4000)
179 {
180 cmd.Parameters.Add("@"+pname,SqlDbType.NVarChar,4000).Value=value;
181 return;
182 }
183 cmd.Parameters.Add("@"+pname,SqlDbType.NText).Value=value;
184 }
185
186 static string _connstr;
187 static SqlConnection CreateConnection()
188 {
189 if(_connstr==null)_connstr=ConfigurationSettings.AppSettings["AspNetMonitorConnectionString"];
190 return new SqlConnection(_connstr);
191 }
192
193
194 NameValueCollection n;
195 string t;
196 public Job(NameValueCollection nvc,string tablename)
197 {
198 n=nvc;
199 t=tablename;
200 }
201 public void Start()
202 {
203 using(SqlConnection conn=CreateConnection())
204 {
205 using(SqlCommand cmd=conn.CreateCommand())
206 {
207 StringBuilder sb1=new StringBuilder();
208 StringBuilder sb2=new StringBuilder();
209 foreach(string key in n.Keys)
210 {
211 AddParameter(cmd,key,n[key]);
212 if(sb1.Length!=0)sb1.Append(",");
213 if(sb2.Length!=0)sb2.Append(",");
214 sb1.Append(key);
215 sb2.Append("@");
216 sb2.Append(key);
217 }
218
219 cmd.CommandText="INSERT INTO "+t+" ( "+sb1+" ) VALUES ( "+sb2+" ) ";
220
221 conn.Open();
222 cmd.ExecuteNonQuery();
223 conn.Close();
224 }
225 }
226 }
227 }
228
229
230 }
231}