Global.asax 的Application_Error事件中加入相关代码,调用的方法是我之前发的那个发送Email的方法:
1 protected void Application_Error(object sender, EventArgs e) 2 { 3 //在出现未处理的错误时运行的代码 4 Exception objErr = Server.GetLastError().GetBaseException(); 5 string time = DateTime.Now.ToString(); 6 string error = ""; 7 error += "<strong>异常信息: </strong>" + objErr.Message + "<br>"; 8 error += "<strong>异常发生时间:</strong>" + time + "<br>"; 9 error += "<strong>IP: </strong>" + Request.UserHostAddress + "<br>"; 10 error += "<strong>发生异常页: </strong>" + Request.Url.ToString() + "<br>"; 11 string url_re = ""; 12 if (Request.UrlReferrer != null) 13 { 14 url_re = Request.UrlReferrer.ToString(); 15 } 16 error += "<strong>上次请求的URL: </strong>" + url_re + "<br>"; 17 error += "<strong>堆栈跟踪: </strong><br>" + objErr.StackTrace.Replace("\r\n", "<br>") + "<br>"; 18 19 20 if (!objErr.Message.Contains("不存在")) 21 { 22 Niunan.ZZ.JKDA.Utility.Tool.SendMail( "网站出现异常"+time, error,"QQ邮箱", "邮箱密码", "smtp.qq.com"); 23 } 24 }
静水,所以流深~