zoukankan      html  css  js  c++  java
  • 一个比较好的.net 3.5的异常报告类

    实现的是当有异常时,发邮件报告 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Net.Mail; 
    namespace Experiement 

        
    public static class MyExtension 
        { 
            
    public static void SendErrorEmail(this Exception ex) 
            { 
                MailMessage mailMessage 
    = new MailMessage(new MailAddress("from@gmail.com"
                                           , 
    new MailAddress("to@gmail.com")); 
                mailMessage.Subject 
    = "Exception Occured in your site"
                mailMessage.IsBodyHtml 
    = true
                System.Text.StringBuilder errorMessage 
    = new System.Text.StringBuilder(); 
                errorMessage.AppendLine(
    string.Format("<B>{0}</B>:{1}","Exception",ex.Message)); 
                errorMessage.AppendLine(
    string.Format("<B>{0}</B>:{1}""Stack Trace", ex.StackTrace)); 
                
    if (ex.InnerException != null
                { 
                    errorMessage.AppendLine(
    string.Format("<B>{0}</B>:{1}"" Inner Exception", ex.InnerException.Message)); 
                    errorMessage.AppendLine(
    string.Format("<B>{0}</B>:{1}""Inner Stack Trace", ex.InnerException.StackTrace)); 
                } 
                mailMessage.Body 
    = errorMessage.ToString(); 
                System.Net.NetworkCredential networkCredentials 
    = new 
                System.Net.NetworkCredential(
    "youraccount@gmail.com""password"); 
                
                SmtpClient smtpClient 
    = new SmtpClient(); 
                smtpClient.EnableSsl 
    = true
                smtpClient.UseDefaultCredentials 
    = false
                smtpClient.Credentials 
    = networkCredentials; 
                smtpClient.Host 
    = "smtp.gmail.com"
                smtpClient.Port 
    = 587
                smtpClient.Send(mailMessage); 
                
            } 
        } 

    使用: 
    using System; 
    namespace Experiement 

        
    public partial class WebForm1 : System.Web.UI.Page 
        { 
            
    protected void Page_Load(object sender,System.EventArgs e) 
            { 
                
    try 
                { 
                    
    throw new Exception("My custom Exception"); 
                } 
                
    catch (Exception ex) 
                { 
                    ex.SendErrorEmail(); 
                    Response.Write(ex.Message); 
                } 
            } 
        } 
    }

  • 相关阅读:
    我爱java系列之---【微服务间的认证—Feign拦截器】
    我爱java系列之---【设置权限的三种解决方案】
    581. Shortest Unsorted Continuous Subarray
    129. Sum Root to Leaf Numbers
    513. Find Bottom Left Tree Value
    515. Find Largest Value in Each Tree Row
    155. Min Stack max stack Maxpop O(1) 操作
    painting house
    Minimum Adjustment Cost
    k Sum
  • 原文地址:https://www.cnblogs.com/lizhao/p/1990423.html
Copyright © 2011-2022 走看看