异常处理 --- B/S下页面的全局异常处理并发送到信箱

一般异常处理,需要在程序中不断的try catch,不断的{}然后不断的调用,可能偶自己比较懒,想找个方便一点的方法,下面就是捕获页面最后一个异常并处理异常的例子:具体做法是,单独一个类,

比如我们叫他PageBase.cs

代码如:

  1. 1using System;
  2. 2
  3. 3namespace netw
  4. 4{
  5. 5 /** <summary>
  6. 6 /// PageBase 的摘要说明。
  7. 7 /// </summary>
  8. 8 public class PageBase:System.Web.UI.Page
  9. 9 {
  10. 10  protected void PageBase_Error(object sender, System.EventArgs e)
  11. 11  {
  12. 12  string errMsg;
  13. 13  //得到系统上一个异常
  14. 14  Exception currentError = Server.GetLastError();
  15. 15
  16. 16  errMsg= "<h1>页面错误</h1><hr/>此页面发现一个意外错误,对此我们非常抱歉。"+
  17. 17    "此错误消息已发送给系统管理员,请及时联系我们,我们会及时解决此问题! <br/>" +
  18. 18    "错误发生位置: "+Request.Url.ToString()+"<br/>"+
  19. 19    "错误消息: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
  20. 20    "<b>Stack Trace:</b><br/>"+
  21. 21    currentError.ToString();
  22. 22
  23. 23  Session["err"]=errMsg;

  24. 24  //如果发生致命应用程序错误
  25. 25  if ( !(currentError is ApplicationException) )
  26. 26  {

  27. 27    //向Windows事件日志中写入错误日志
  28. 28    //LogEvent( currentError.ToString(), EventLogEntryType.Error );
  29. 29  }

  30. 30  //在页面中显示错误31  //Response.Write( errMsg );
  31. 32          Response.Redirect("error.aspx");

  32. 33  //清除异常
  33. 34  Server.ClearError();
  34. 35  }
  35. 36
  36. 37       
  37. 38  private void PageBase_Load(object sender, System.EventArgs e)
  38. 39  {
  39. 40
  40. 41  }
  41. 42  protected override void OnInit(EventArgs e)
  42. 43  {
  43. 44  base.OnInit(e);
  44. 45  this.Error += new System.EventHandler(this.PageBase_Error);
  45. 46  }
  46. 47
  47. 48 }

这里是把出现异常的地址和具体信息发送到Response.Redirect("error.aspx"); 而error.aspx呢,我是把错误信息发送到自己信箱的,以便及时修正,没想到平均没天我信箱里就要收到200多封错误报告,清一色的数据库路径不对,

代码如:需要引入using System.Web.Mail;空间

  1. 1private void Page_Load(object sender, System.EventArgs e)
  2. 2  {
  3. 3  sendemadil();
  4. 4  Label1.Text=Session["err"].ToString();
  5. 5  }
  6. 6  private void sendemadil()
  7. 7  {
  8. 8  MailMessage msg = new MailMessage();

  9. 9  //发送方地址(如test@163.com)
  10. 10  msg.From ="madyina@163.com";

  11. 11  //接收方地址(如test@163.com)
  12. 12  msg.To =ConfigurationSettings.AppSettings["themail"];

  13. 13  //正文内容类型14  msg.BodyFormat = MailFormat.Html;

  14. 15  //正文内容编码
  15. 16  msg.BodyEncoding = System.Text.Encoding.Default;

  16. 17  //主题
  17. 18    msg.Subject ="网站错误报告";

  18. 19  //内容
  19. 20    msg.Body =Session["err"].ToString();
  20. 21

  21. 22  //设置为需要用户验证
  22. 23  msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );

  23. 24  //设置验证用户名
  24. 25  msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "你的信箱用户名");//用于验证的,不是接收的

  25. 26  //设置验证密码
  26. 27  msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "你的信箱密码");
  27. 28

  28. 29  //邮件服务器地址(如smtp.163.com)
  29. 30  SmtpMail.SmtpServer = "smtp.163.com";
  30. 31

  31. 32  //发送
  32. 33  SmtpMail.Send(msg);
  33. 34  }
  34. 35
  35. 36
需要处理异常的页面只需继承下这个类就可以了
继承示例:public class index ageBase 在需要的页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值