(求助)即时发送异常给开发人员

在项目完成初期,用户使用不可避免的会发生错误或者异常,
如何使开发人员更及时更准确地获得用户使用的情况,
会对项目质量,进度,客户满意程度产生不小的影响。

所以,我想实现这样一个功能,当程序执行中,捕获异常,
将异常信息和发生异常的位置,通过Email即时发送给开发人员。
目前实现了基本的功能,但是,存在一点问题,用户使用中,
没有理由把发送email的时间也由用户来负担。
那么通过哪种处理方式可以更合理呢?

下面是现在完成的部分,只有记录日志和发送邮件的部分..(发送邮件只做完了不需要验证的smtp部分.11.gif).

1.记录日志   如果发送邮件失败,记录未发送的异常信息.

 1 None.gif
 2 ExpandedBlockStart.gifContractedBlock.gif /**/ /// <summary>
 3InBlock.gif/// Record info to path
 4ExpandedBlockEnd.gif/// </summary>

 5 None.gif internal   class  LogWriter
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7ContractedSubBlock.gifExpandedSubBlockStart.gif    structure#region structure
 8InBlock.gif    private LogWriter(string path)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        m_path = path;
11ExpandedSubBlockEnd.gif    }

12InBlock.gif
13InBlock.gif    static public LogWriter m_Writer = null;
14InBlock.gif    static public LogWriter CreateInstance(string path)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        if (m_Writer == null)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            m_Writer = new LogWriter(path);
19ExpandedSubBlockEnd.gif        }

20InBlock.gif        return m_Writer;
21ExpandedSubBlockEnd.gif    }

22ExpandedSubBlockEnd.gif    #endregion

23InBlock.gif
24InBlock.gif    private string m_path = "";
25InBlock.gif
26ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
27InBlock.gif    /// Record normal info
28InBlock.gif    /// </summary>
29ExpandedSubBlockEnd.gif    /// <param name="message"></param>

30InBlock.gif    internal void WriteInfo(string message)
31ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
32InBlock.gif        StreamWriter msgWriter = new StreamWriter(m_path, true, System.Text.Encoding.Default);
33InBlock.gif
34InBlock.gif        msgWriter.WriteLine(DateTime.Now + ":" + message + "\r\n");
35InBlock.gif
36InBlock.gif        msgWriter.Flush();
37InBlock.gif        msgWriter.Close();
38InBlock.gif        msgWriter.Dispose();
39ExpandedSubBlockEnd.gif    }

40InBlock.gif    
41ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
42InBlock.gif    /// Record exception info 
43InBlock.gif    /// </summary>
44ExpandedSubBlockEnd.gif    /// <param name="ex"></param>

45InBlock.gif    internal void WriteInfo(Exception ex)
46ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
47InBlock.gif        string eString = ex.ToString();
48InBlock.gif        WriteInfo(eString);
49ExpandedSubBlockEnd.gif    }

50InBlock.gif    
51ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
52InBlock.gif    /// Validate the filePath available..
53InBlock.gif    /// </summary>
54ExpandedSubBlockEnd.gif    /// <param name="path"></param>

55InBlock.gif    internal void ValidatePath(string path)
56ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
57InBlock.gif        if (!File.Exists(path))
58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
59InBlock.gif            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
60InBlock.gif
61InBlock.gif            fs.Flush();
62InBlock.gif            fs.Close();
63InBlock.gif            fs.Dispose();
64ExpandedSubBlockEnd.gif        }

65ExpandedSubBlockEnd.gif    }

66ExpandedBlockEnd.gif}

67 None.gif


2.通过email发送异常信息.先检查记录位置是否有未发送信息,如果有,发送.

  1 None.gif
  2 ExpandedBlockStart.gifContractedBlock.gif /**/ /// <summary>
  3InBlock.gif/// Class for email Exception info to developer..
  4ExpandedBlockEnd.gif/// </summary>

  5 None.gif public   class  ExceptionMail
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ContractedSubBlock.gifExpandedSubBlockStart.gif    structure#region structure
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    private ExceptionMail()dot.gif{}
  9InBlock.gif 
 10InBlock.gif    static public ExceptionMail CreateInstance()
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        return new ExceptionMail();
 13ExpandedSubBlockEnd.gif    }

 14ExpandedSubBlockEnd.gif   #endregion

 15InBlock.gif
 16InBlock.gif    // Get file path from configuration
 17InBlock.gif    static readonly private string logPath = ConfigurationManager.AppSettings["logPath"];
 18InBlock.gif
 19InBlock.gif    // SmtpClient object to send email
 20InBlock.gif    private SmtpClient mailSender = new SmtpClient("smtp.?????.com"25);
 21InBlock.gif    
 22ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 23InBlock.gif    /// Send message by email
 24InBlock.gif    /// </summary>
 25InBlock.gif    /// <param name="subject">subject of email</param>
 26InBlock.gif    /// <param name="message">content of email</param>
 27ExpandedSubBlockEnd.gif    /// <returns></returns>

 28InBlock.gif    public bool SendMail(string subject, string message)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 30InBlock.gif        SendUnsettled();
 31InBlock.gif
 32InBlock.gif        AuthorizeSmtp(mailSender);
 33InBlock.gif
 34InBlock.gif        try
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 36InBlock.gif            mailSender.Send("XXXXX@XXXXX.com""XXXXX@XXXXX.com", subject + DateTime.Now.ToString(), message);
 37InBlock.gif
 38InBlock.gif            return true;
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif        catch
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            LogWriter.CreateInstance(logPath).WriteInfo(message);
 43InBlock.gif
 44InBlock.gif            return false;
 45ExpandedSubBlockEnd.gif        }

 46ExpandedSubBlockEnd.gif    }

 47InBlock.gif    
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 49InBlock.gif    /// Send exception by email 
 50InBlock.gif    /// </summary>
 51InBlock.gif    /// <param name="exception">Exception that would be sent</param>
 52ExpandedSubBlockEnd.gif    /// <returns></returns>

 53InBlock.gif    public bool SendException(Exception exception)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 55InBlock.gif        SendUnsettled();
 56InBlock.gif
 57InBlock.gif        AuthorizeSmtp(mailSender);
 58InBlock.gif
 59InBlock.gif    string errorInfo = "Message:" + exception.Message + "\r\n" + "Detail:" + exception.ToString();
 60InBlock.gif
 61InBlock.gif        try
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 63InBlock.gif            mailSender.Send("XXXXX@XXXXX.com""XXXXX@XXXXX.com""Exception:" + DateTime.Now.ToString(),
 64InBlock.gif
 65InBlock.gif                errorInfo);
 66InBlock.gif
 67InBlock.gif            return true;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif        catch
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            LogWriter.CreateInstance(logPath).WriteInfo(errorInfo);            
 72InBlock.gif            return false;
 73ExpandedSubBlockEnd.gif        }

 74ExpandedSubBlockEnd.gif    }

 75InBlock.gif    
 76ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 77InBlock.gif    /// Add credential to make sender authorized  
 78InBlock.gif    /// </summary>
 79ExpandedSubBlockEnd.gif    /// <param name="smtp">sender</param>

 80InBlock.gif    private void AuthorizeSmtp(SmtpClient smtp)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 82InBlock.gif        smtp.Credentials = new NetworkCredential("?????""?????");
 83InBlock.gif
 84InBlock.gif        smtp.UseDefaultCredentials = true;
 85InBlock.gif
 86InBlock.gif        smtp.Timeout = 10000;
 87InBlock.gif
 88InBlock.gif        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
 89ExpandedSubBlockEnd.gif    }

 90InBlock.gif    
 91ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 92InBlock.gif    /// Deal with message unsettleddot.gif 
 93ExpandedSubBlockEnd.gif    /// </summary>

 94InBlock.gif    private void SendUnsettled()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif        if (File.Exists(logPath))
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            StreamReader logReader = new StreamReader(logPath, System.Text.Encoding.Default);
 99InBlock.gif
100InBlock.gif            string message = "";
101InBlock.gif
102InBlock.gif            if (logReader.Peek() >= 0)
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                message += logReader.ReadLine() + "\r\n";
105ExpandedSubBlockEnd.gif            }

106InBlock.gif
107InBlock.gif            SendMail("Unsettled Log", message);
108InBlock.gif
109InBlock.gif            logReader.Close();
110InBlock.gif            logReader.Dispose();
111InBlock.gif            logReader = null;
112InBlock.gif            File.Delete(logPath);
113ExpandedSubBlockEnd.gif        }

114ExpandedSubBlockEnd.gif    }

115ExpandedBlockEnd.gif}
  
116 None.gif

3.暂存未发送emal的日志文件保存路径,在config文件里加入一个logPath配置项保存.

仔细想了想,可以把发送异常的方法作为独立的进程运行,这样就不会在发生异常

的时候阻塞下面的操作.

22.gif 22.gif 22.gif

转载于:https://www.cnblogs.com/snowlove67/archive/2006/02/09/327523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值