You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.2 KiB

8 months ago
  1. using NLog;
  2. using System;
  3. using System.Text.Json;
  4. namespace Mirle.Component.Record
  5. {
  6. /// <summary>
  7. /// 記錄錯誤擴充方法類別
  8. /// </summary>
  9. public static class LogErrorExtension
  10. {
  11. /// <summary>
  12. /// 紀錄錯誤資訊
  13. /// </summary>
  14. /// <param name="log">紀錄器</param>
  15. /// <param name="funcName">方法名稱</param>
  16. /// <param name="ex">例外錯誤</param>
  17. /// <param name="param">參數</param>
  18. public static void LogError(this Logger log, string funcName, Exception ex, string param = "None")
  19. {
  20. log.Error($"[{funcName}] | [{param}] | {ex.Message}");
  21. log.Error($"========= Error Stack Start ====== \r\n " +
  22. $"{ex.StackTrace} \r\n" +
  23. $"========= Error Stack End ========");
  24. }
  25. /// <summary>
  26. /// 記錄錯誤資訊
  27. /// </summary>
  28. /// <param name="log">紀錄器</param>
  29. /// <param name="funcName">方法名稱</param>
  30. /// <param name="errorMessage">錯誤訊息</param>
  31. /// <param name="param">參數</param>
  32. public static void LogError(this Logger log, string funcName, string errorMessage, string param = "None")
  33. {
  34. log.Error($"[{funcName}] | [{param}] | {errorMessage}");
  35. }
  36. /// <summary>
  37. /// 記錄錯誤資料
  38. /// </summary>
  39. /// <typeparam name="T">資料類別</typeparam>
  40. /// <param name="log">紀錄器</param>
  41. /// <param name="tableName">方法名稱</param>
  42. /// <param name="data">錯誤資料</param>
  43. /// <param name="ex">例外錯誤</param>
  44. /// <param name="param">參數</param>
  45. public static void LogErrorData<T>(this Logger log, string tableName, T data, Exception ex, string param = "None")
  46. {
  47. log.Error($"Error Data: {JsonSerializer.Serialize(data)}");
  48. log.Error($"[false] | [{tableName}] | {ex.Message} \r\n" +
  49. $"========= Error Stack Start ========== \r\n" +
  50. $"{ex.StackTrace} \r\n" +
  51. $"========= Error Stack End ============");
  52. }
  53. }
  54. }