项目中报错邮件方法

时间:2022-11-04 15:58:31

项目中使用报错邮件的重要性,O(∩_∩)O~嘿嘿...重要性我就不多说,只说一点:可以帮助你快速定位项目中没有被捕获的BUG...这个报错邮件的方法一般都是添加在一个单独的类(例如BaseClass)中,给取数据的接口,登陆,详情页,...这些要害"部门"使用,这些页面的.cs文件都继承至这个类(BaseClass).

 override protected void OnInit(EventArgs e)
      {
          InitializeComponent();
          base.OnInit(e);
      }
  
      private void InitializeComponent()
      {
          Error += Page_Error;
     }
 
     void Page_Error(object sender, EventArgs e)
     {
         //可以添加一个缓存
         string CacheKey = "errorinfo";
         try
         {
             Exception ex = Server.GetLastError();
             string errMsg = ex.Message;
             string param = string.Empty;
             string url = HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath + errMsg;
 
             Hashtable ErrorTable = HttpContext.Current.Cache[CacheKey] as Hashtable;
             if (ErrorTable == null)
             {
                ErrorTable = new Hashtable();
                 HttpContext.Current.Cache.Insert(CacheKey, ErrorTable, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
             }
             if (ErrorTable.Contains(url)) 
             { 
                 return;
            }
             else
             {
                 //没找到
                 ErrorTable.Add(url, errMsg);
             }
 
             if (Request.Form != null)
                 param += Environment.NewLine + "接收Form:" + Server.UrlDecode(Request.Form.ToString());
 
             if (Request.QueryString != null)
                 param += Environment.NewLine + "接收QueryString:" + Server.UrlDecode(Request.QueryString.ToString());
 
             if (Request.UrlReferrer != null)
             {
                 errMsg += Environment.NewLine + "来源:" + Request.UrlReferrer.AbsoluteUri;
             }
 
             //这个收件人邮箱可以在配置文件中配置: <add key="EmailTo" value="renyong@soufun.com,bilingyun@soufun.com"></add>
             //string mailTo = ConfigurationManager.AppSettings["EmailTo"];
             //也可以直接写:
             string mailTo = 123@126.com,456@126.com;
            if (string.IsNullOrEmpty(mailTo))
             {
                 mailTo = 123@soufun.com;
             }
 
             string[] arrMailTo = mailTo.Split(',');
             //这个地方的发件人,一写成你项目的邮箱地址..(也可以随意配一个你知道的邮箱地址...O(∩_∩)O~)
             MailMessage mm = new MailMessage(new MailAddress("issosystem@126.com"), new MailAddress(arrMailTo[0]));
             int arrLength = arrMailTo.Length;
             if (arrLength > 0)
             {
                 for (int i = 0; i < arrLength; i++)
                 {
                     mm.To.Add(new MailAddress(arrMailTo[i]));
                 }
             }
 
             mm.Subject = HttpContext.Current.Request.ServerVariables["Local_addr"] + "  " + Request.Url.Authority + "   项目报错信息";//
             StringBuilder sbBody = new StringBuilder("发生时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
             sbBody.Append(Environment.NewLine + "发生异常页:" + HttpContext.Current.Request.Url.AbsoluteUri);
             if (HttpContext.Current.Request.UrlReferrer != null)
             {
                 sbBody.Append(Environment.NewLine + "访问者来源:" + "<a href=\" " + HttpContext.Current.Request.UrlReferrer.AbsoluteUri + " \">" + HttpContext.Current.Request.UrlReferrer.AbsoluteUri + "</a>");
             }
             else
             {                 sbBody.Append(Environment.NewLine + "没有访问者来源信息");
             }
             sbBody.Append(Environment.NewLine + "访问者IP:" + Common.GetUserIP());
             sbBody.Append(Environment.NewLine + "服务器IP:" + HttpContext.Current.Request.ServerVariables["Local_addr"]);
             sbBody.Append(Environment.NewLine + "参数信息:" + param);
             sbBody.Append(Environment.NewLine + "错误信息:" + errMsg);
             sbBody.Append(Environment.NewLine + ex.StackTrace);
             mm.Body = sbBody.ToString();
             //需要设置一个邮件服务器...
             SmtpClient sendBox = new SmtpClient("mail.126.com");//你公司的邮件服务器
             sendBox.Send(mm);
 
         }
         catch (Exception exe)
         {
             string aa = exe.Message;
         }
     }