免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
自定義簡單日志記錄類

實(shí)在被log4net折磨的無語了,就自己寫了一個(gè)簡單的日志記錄操作類。

源碼如下(VS2015):

  1. /**************************************************
  2. *
  3. *命名空間: Common
  4. * 類名: FuncMyLog
  5. * 作者: 賈勝杰(2017/11/24/周五 9:22:34 )
  6. *模塊說明: 自定義日志類
  7. *修改日志:
  8. *
  9. **************************************************/
  10. using System;
  11. using System.IO;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. namespace Common
  15. {
  16. public static class FuncMyLog
  17. {
  18. /// <summary>
  19. /// 這里需要手動(dòng)修改 ErrorLog 是我在本地的日志文件夾名稱
  20. /// </summary>
  21. private static readonly string StrPath = Path.Combine(Application.StartupPath,"ErrorLog");
  22. /// <summary>
  23. /// 創(chuàng)建路徑 按天保存
  24. /// </summary>
  25. /// <returns></returns>
  26. private static string CreatePath()
  27. {
  28. if (!Directory.Exists(StrPath))
  29. Directory.CreateDirectory(StrPath);
  30. return $"{StrPath}\\{DateTime.Now.ToString("yyyy-MM-dd")}.log";
  31. }
  32. /// <summary>
  33. /// 以流的方式保存文件
  34. /// </summary>
  35. /// <param name="strInfo">文本信息</param>
  36. private static void SaveDataLog(string strInfo)
  37. {
  38. StringBuilder sb = new StringBuilder();
  39. sb.Append($"***************記錄時(shí)間【");
  40. sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
  41. sb.Append("】***************");
  42. sb.AppendLine(strInfo);
  43. StreamWriter sw = new StreamWriter(CreatePath(), true, Encoding.Default);
  44. sw.Write(sb);
  45. sw.Flush();
  46. sw.Close();
  47. }
  48. /// <summary>
  49. /// 寫日志
  50. /// </summary>
  51. /// <param name="strInfo">錯(cuò)誤信息</param>
  52. /// <param name="isDirect">是否需要直接保存 默認(rèn)為否(后臺(tái)處理)</param>
  53. public static void WriteLog(string strInfo,bool isDirect=false)
  54. {
  55. SaveDataLog(isDirect ? $"詳細(xì)信息:{strInfo}" : strInfo);
  56. }
  57. /// <summary>
  58. /// 寫日志
  59. /// </summary>
  60. /// <param name="ex">Exception 實(shí)例</param>
  61. public static void WriteLog(Exception ex)
  62. {
  63. if (string.IsNullOrWhiteSpace(ex?.StackTrace)) return;
  64. string strStack = ex.StackTrace;
  65. //獲取異常的開始方法
  66. int index1 = strStack.LastIndexOf(" 在 ", StringComparison.Ordinal);
  67. int index2 = strStack.LastIndexOf(" 位置 ", StringComparison.Ordinal);
  68. int indexMove = 3;
  69. string strMethod = strStack.Substring(index1 + indexMove, index2 - index1 - indexMove);
  70. //獲取異常開始方法的位置
  71. int index3 = strStack.LastIndexOf("行號(hào) ", StringComparison.Ordinal);
  72. string strRowIndex = strStack.Substring(index3 + indexMove, strStack.Length - index3 - indexMove);
  73. StringBuilder sb = new StringBuilder();
  74. sb.AppendLine();
  75. sb.Append($"出錯(cuò)位置:{strMethod} ");
  76. sb.Append($"(第{strRowIndex}行)");
  77. sb.AppendLine();
  78. sb.AppendLine($"異常信息:{ex.Message}");
  79. sb.AppendLine($"詳細(xì)信息:{ex.StackTrace}");
  80. SaveDataLog(sb.ToString());
  81. }
  82. }
  83. }

調(diào)用示例:

  1. private void TestLog()
  2. {
  3. try
  4. {
  5. int a = 3;
  6. int b = 0;
  7. int c = a / b;
  8. }
  9. catch (Exception ex)
  10. {
  11. FuncMyLog.WriteLog(ex);
  12. }
  13. }
效果圖:


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
WinForm如何優(yōu)雅的處理異常
關(guān)于.NET異常處理的思考
我們是怎么實(shí)現(xiàn)gRPC CodeFirst-生成proto
擴(kuò)展ASP.NET MVC HtmlHelper類(翻的)
C#
Java 最新支付版本 demo 微信支付
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服