zoukankan      html  css  js  c++  java
  • 项目中报错邮件方法

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

    View Code
     1 override protected void OnInit(EventArgs e)
     2     {
     3         InitializeComponent();
     4         base.OnInit(e);
     5     }
     6 
     7     private void InitializeComponent()
     8     {
     9         Error += Page_Error;
    10     }
    11 
    12     void Page_Error(object sender, EventArgs e)
    13     {
    14         //可以添加一个缓存
    15         string CacheKey = "errorinfo";
    16         try
    17         {
    18             Exception ex = Server.GetLastError();
    19             string errMsg = ex.Message;
    20             string param = string.Empty;
    21             string url = HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath + errMsg;
    22 
    23             Hashtable ErrorTable = HttpContext.Current.Cache[CacheKey] as Hashtable;
    24             if (ErrorTable == null)
    25             {
    26                 ErrorTable = new Hashtable();
    27                 HttpContext.Current.Cache.Insert(CacheKey, ErrorTable, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
    28             }
    29             if (ErrorTable.Contains(url)) 
    30             { 
    31                 return;
    32             }
    33             else
    34             {
    35                 //没找到
    36                 ErrorTable.Add(url, errMsg);
    37             }
    38 
    39             if (Request.Form != null)
    40                 param += Environment.NewLine + "接收Form:" + Server.UrlDecode(Request.Form.ToString());
    41 
    42             if (Request.QueryString != null)
    43                 param += Environment.NewLine + "接收QueryString:" + Server.UrlDecode(Request.QueryString.ToString());
    44 
    45             if (Request.UrlReferrer != null)
    46             {
    47                 errMsg += Environment.NewLine + "来源:" + Request.UrlReferrer.AbsoluteUri;
    48             }
    49 
    50             //这个收件人邮箱可以在配置文件中配置: <add key="EmailTo" value="renyong@soufun.com,bilingyun@soufun.com"></add>
    51             //string mailTo = ConfigurationManager.AppSettings["EmailTo"];
    52             //也可以直接写:
    53             string mailTo = "123@126.com,456@126.com";
    54             if (string.IsNullOrEmpty(mailTo))
    55             {
    56                 mailTo = "123@soufun.com";
    57             }
    58 
    59             string[] arrMailTo = mailTo.Split(',');
    60             //这个地方的发件人,一写成你项目的邮箱地址..(也可以随意配一个你知道的邮箱地址...O(∩_∩)O~)
    61             MailMessage mm = new MailMessage(new MailAddress("issosystem@126.com"), new MailAddress(arrMailTo[0]));
    62             int arrLength = arrMailTo.Length;
    63             if (arrLength > 0)
    64             {
    65                 for (int i = 0; i < arrLength; i++)
    66                 {
    67                     mm.To.Add(new MailAddress(arrMailTo[i]));
    68                 }
    69             }
    70 
    71             mm.Subject = HttpContext.Current.Request.ServerVariables["Local_addr"] + "  " + Request.Url.Authority + "   项目报错信息";//
    72             StringBuilder sbBody = new StringBuilder("发生时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    73             sbBody.Append(Environment.NewLine + "发生异常页:" + HttpContext.Current.Request.Url.AbsoluteUri);
    74             if (HttpContext.Current.Request.UrlReferrer != null)
    75             {
    76                 sbBody.Append(Environment.NewLine + "访问者来源:" + "<a href=\" " + HttpContext.Current.Request.UrlReferrer.AbsoluteUri + " \">" + HttpContext.Current.Request.UrlReferrer.AbsoluteUri + "</a>");
    77             }
    78             else
    79             {
    80                 sbBody.Append(Environment.NewLine + "没有访问者来源信息");
    81             }
    82             sbBody.Append(Environment.NewLine + "访问者IP:" + Common.GetUserIP());
    83             sbBody.Append(Environment.NewLine + "服务器IP:" + HttpContext.Current.Request.ServerVariables["Local_addr"]);
    84             sbBody.Append(Environment.NewLine + "参数信息:" + param);
    85             sbBody.Append(Environment.NewLine + "错误信息:" + errMsg);
    86             sbBody.Append(Environment.NewLine + ex.StackTrace);
    87             mm.Body = sbBody.ToString();
    88             //需要设置一个邮件服务器...
    89             SmtpClient sendBox = new SmtpClient("mail.126.com");//你公司的邮件服务器
    90             sendBox.Send(mm);
    91 
    92         }
    93         catch (Exception exe)
    94         {
    95             string aa = exe.Message;
    96         }
    97     }
  • 相关阅读:
    requests使用text可以查看源码
    正则表达式之search、sub
    【JS】深拷贝与浅拷贝的区别,实现深拷贝的几种方法
    php:对象(object)数据类型实例详解
    usage: git remote add [<options>] <name> <url> -f, --fetch fetch the remote branches --tags import all tags and associated objects when fetching
    PHP 可选参数
    php中文乱码问题的终极解决方案汇总
    html表单提交给PHP然后浏览器显示出了PHP的源代码
    wamp 安装
    wamp选择语言
  • 原文地址:https://www.cnblogs.com/ry123/p/2550421.html
Copyright © 2011-2022 走看看