(求助)即时发送异常给开发人员
生活随笔
收集整理的這篇文章主要介紹了
(求助)即时发送异常给开发人员
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在項目完成初期,用戶使用不可避免的會發生錯誤或者異常,
如何使開發人員更及時更準確地獲得用戶使用的情況,
會對項目質量,進度,客戶滿意程度產生不小的影響。
所以,我想實現這樣一個功能,當程序執行中,捕獲異常,
將異常信息和發生異常的位置,通過Email即時發送給開發人員。
目前實現了基本的功能,但是,存在一點問題,用戶使用中,
沒有理由把發送email的時間也由用戶來負擔。
那么通過哪種處理方式可以更合理呢?
下面是現在完成的部分,只有記錄日志和發送郵件的部分..(發送郵件只做完了不需要驗證的smtp部分.).
1.記錄日志?? 如果發送郵件失敗,記錄未發送的異常信息.
?2/**////?<summary>
?3///?Record?info?to?path
?4///?</summary>
?5internal?class?LogWriter
?6{
?7????structure#region?structure
?8????private?LogWriter(string?path)
?9????{
10????????m_path?=?path;
11????}
12
13????static?public?LogWriter?m_Writer?=?null;
14????static?public?LogWriter?CreateInstance(string?path)
15????{
16????????if?(m_Writer?==?null)
17????????{
18????????????m_Writer?=?new?LogWriter(path);
19????????}
20????????return?m_Writer;
21????}
22????#endregion
23
24????private?string?m_path?=?"";
25
26????/**////?<summary>
27????///?Record?normal?info
28????///?</summary>
29????///?<param?name="message"></param>
30????internal?void?WriteInfo(string?message)
31????{
32????????StreamWriter?msgWriter?=?new?StreamWriter(m_path,?true,?System.Text.Encoding.Default);
33
34????????msgWriter.WriteLine(DateTime.Now?+?":"?+?message?+?"\r\n");
35
36????????msgWriter.Flush();
37????????msgWriter.Close();
38????????msgWriter.Dispose();
39????}
40????
41????/**////?<summary>
42????///?Record?exception?info?
43????///?</summary>
44????///?<param?name="ex"></param>
45????internal?void?WriteInfo(Exception?ex)
46????{
47????????string?eString?=?ex.ToString();
48????????WriteInfo(eString);
49????}
50????
51????/**////?<summary>
52????///?Validate?the?filePath?available..
53????///?</summary>
54????///?<param?name="path"></param>
55????internal?void?ValidatePath(string?path)
56????{
57????????if?(!File.Exists(path))
58????????{
59????????????FileStream?fs?=?new?FileStream(path,?FileMode.Create,?FileAccess.ReadWrite,?FileShare.ReadWrite);
60
61????????????fs.Flush();
62????????????fs.Close();
63????????????fs.Dispose();
64????????}
65????}
66}
67
2.通過email發送異常信息.先檢查記錄位置是否有未發送信息,如果有,發送.
??2/**////?<summary>
??3///?Class?for?email?Exception?info?to?developer..
??4///?</summary>
??5public?class?ExceptionMail
??6{
??7????structure#region?structure
??8????private?ExceptionMail(){}
??9?
?10????static?public?ExceptionMail?CreateInstance()
?11????{
?12????????return?new?ExceptionMail();
?13????}
?14???#endregion
?15
?16????//?Get?file?path?from?configuration
?17????static?readonly?private?string?logPath?=?ConfigurationManager.AppSettings["logPath"];
?18
?19????//?SmtpClient?object?to?send?email
?20????private?SmtpClient?mailSender?=?new?SmtpClient("smtp.?????.com",?25);
?21????
?22????/**////?<summary>
?23????///?Send?message?by?email
?24????///?</summary>
?25????///?<param?name="subject">subject?of?email</param>
?26????///?<param?name="message">content?of?email</param>
?27????///?<returns></returns>
?28????public?bool?SendMail(string?subject,?string?message)
?29????{
?30????????SendUnsettled();
?31
?32????????AuthorizeSmtp(mailSender);
?33
?34????????try
?35????????{
?36????????????mailSender.Send("XXXXX@XXXXX.com",?"XXXXX@XXXXX.com",?subject?+?DateTime.Now.ToString(),?message);
?37
?38????????????return?true;
?39????????}
?40????????catch
?41????????{
?42????????????LogWriter.CreateInstance(logPath).WriteInfo(message);
?43
?44????????????return?false;
?45????????}
?46????}
?47????
?48????/**////?<summary>
?49????///?Send?exception?by?email?
?50????///?</summary>
?51????///?<param?name="exception">Exception?that?would?be?sent</param>
?52????///?<returns></returns>
?53????public?bool?SendException(Exception?exception)
?54????{
?55????????SendUnsettled();
?56
?57????????AuthorizeSmtp(mailSender);
?58
?59????string?errorInfo?=?"Message:"?+?exception.Message?+?"\r\n"?+?"Detail:"?+?exception.ToString();
?60
?61????????try
?62????????{????????????
?63????????????mailSender.Send("XXXXX@XXXXX.com",?"XXXXX@XXXXX.com",?"Exception:"?+?DateTime.Now.ToString(),
?64
?65????????????????errorInfo);
?66
?67????????????return?true;
?68????????}
?69????????catch
?70????????{
?71????????????LogWriter.CreateInstance(logPath).WriteInfo(errorInfo);????????????
?72????????????return?false;
?73????????}
?74????}
?75????
?76????/**////?<summary>
?77????///?Add?credential?to?make?sender?authorized??
?78????///?</summary>
?79????///?<param?name="smtp">sender</param>
?80????private?void?AuthorizeSmtp(SmtpClient?smtp)
?81????{
?82????????smtp.Credentials?=?new?NetworkCredential("?????",?"?????");
?83
?84????????smtp.UseDefaultCredentials?=?true;
?85
?86????????smtp.Timeout?=?10000;
?87
?88????????smtp.DeliveryMethod?=?SmtpDeliveryMethod.Network;
?89????}
?90????
?91????/**////?<summary>
?92????///?Deal?with?message?unsettled?
?93????///?</summary>
?94????private?void?SendUnsettled()
?95????{
?96????????if?(File.Exists(logPath))
?97????????{
?98????????????StreamReader?logReader?=?new?StreamReader(logPath,?System.Text.Encoding.Default);
?99
100????????????string?message?=?"";
101
102????????????if?(logReader.Peek()?>=?0)
103????????????{
104????????????????message?+=?logReader.ReadLine()?+?"\r\n";
105????????????}
106
107????????????SendMail("Unsettled?Log",?message);
108
109????????????logReader.Close();
110????????????logReader.Dispose();
111????????????logReader?=?null;
112????????????File.Delete(logPath);
113????????}
114????}
115}??
116
3.暫存未發送emal的日志文件保存路徑,在config文件里加入一個logPath配置項保存.
仔細想了想,可以把發送異常的方法作為獨立的進程運行,這樣就不會在發生異常
的時候阻塞下面的操作.
轉載于:https://www.cnblogs.com/snowlove67/archive/2006/02/09/327523.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的(求助)即时发送异常给开发人员的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: I've got so many hon
- 下一篇: 几个以前项目中使用的函数 (转)