[原创]自己写的一个反馈异常用的类
2007-10-01 18:29 水随风 阅读(276) 评论(0) 收藏 举报没事写个,以后可能会用到,哈哈;
============异常========反馈==========反馈异常用的类======waterlion====
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Web.UI;
using System.Web.Mail;
using System.Net;
using System.Web;
namespace SendErrorInfo
{
    /// <summary>
    /// 水随风 2007-10-1
    /// 本类主要用于对程序中代码段的异常做出记录和反馈
    /// 从中可以了解到哪些类有异常,可以更好的优化和完善程序
    /// 此程序称为 “处理异常探针”
    /// 引用类为 System.Net.Mail
    /// 主要用到方法为 SmtpClient和MailMessage类
    /// webconfig中appsetting中配置SmtpServerAddress,Mailto,Mailfrom
    /// </summary>
    public static  class SendError
    {
        /// <summary>
        /// 发送邮件
        /// 使用此方法要在webconfig中appsetting中配置SmtpServerAddress,Mailto,Mailfrom
        /// </summary>
        /// <param name="ex"></param>
        public static void SendMessage( Exception ex ,string errorlibrary)
        {
            string SmtpServerAddress = ConfigurationManager.AppSettings["SmtpServerAddress"];
            SmtpMail.SmtpServer = SmtpServerAddress;
            MailMessage mailMessage = new MailMessage();
            mailMessage.From=ConfigurationManager.AppSettings["Mailfrom"].ToString();
            mailMessage.To=ConfigurationManager.AppSettings["MailTo"].ToString();
            mailMessage.Subject = "网站后台异常反馈邮件!";
            mailMessage.Body = @"<html><head><title></title></head><body><fieldSet><legend><font color='red'>网站异常反馈邮件</font></legend>时间:" + DateTime.Now + @"<br/>出错层:" + errorlibrary + @"<br/>出错信息:"+ex.Message.ToString()+@"<br/></fieldSet></body></html>";
            mailMessage.BodyFormat = MailFormat.Html;
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   //身份验证   
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yzx_5789@tom.com");   //设置你的邮箱
            mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******");   //设置你的密码            SmtpMail.Send(mailMessage);
         }
        /// <summary>
        /// 写到服务器日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="errorlibrary"></param>
        public static void WriterLog(Exception ex, string errorlibrary)
        {
            try
            {
                Request.ServerVariables["APPL_PHYSICAL_PATH"]; 
                if (!Directory.Exists("log"))
                {
                    Directory.CreateDirectory("log");
                }
                if (!File.Exists("Gcxysytem.log"))
                {
                    File.Create("Gcxysytem.log");
                }
                FileInfo finfo=new FileInfo("/log/Gcxysystem.log");
                using(FileStream fs = finfo.OpenWrite())
              { 
             /**////根据上面创建的文件流创建写数据流
             StreamWriter sw = new StreamWriter(fs);
               
             /**////设置写数据流的起始位置为文件流的末尾
             sw.BaseStream.Seek(0, SeekOrigin.End);
             /**////写入“Debug Info: ”
             sw.Write("Debug: ");
             /**////写入当前系统时间
             sw.Write("--{0} {1}--", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()); 
             /**////写入日志内容并换行
                 sw.Write("(" + errorlibrary + ") <" + ex.Message + ">\r\n"); 
             sw.Write("<------------------------------------------------------------------------------------->\r\n");
             /**////清空缓冲区内容,并把缓冲区内容写入基础流
             sw.Flush();
             /**////关闭写数据流
             sw.Close();///////////////
             }
            }
        catch(Exception ex1)
        {
             string strEx = ex1.ToString(); 
        }
        }
        public static void SendAll(Exception ex, string errorlibrary)
        {
            SendMessage(ex,errorlibrary);
            WriterLog(ex, errorlibrary);
        }
    }
}
编译好的DLL下载:SendErrorInfo
============异常========反馈==========反馈异常用的类======waterlion====
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号