LogHelper.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XdCxRhDW.Dto;
  8. namespace X1LeoTaskServer54
  9. {
  10. public static class LogHelper
  11. {
  12. public static Action<LogInfo> Logger;
  13. public static string BaseUrl;
  14. public static async Task Info(string msg)
  15. {
  16. var pro = Process.GetCurrentProcess();
  17. try
  18. {
  19. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  20. {
  21. LogTime = DateTime.Now,
  22. LogType = EnumLogTypeDto.Info,
  23. Module = pro.ProcessName,
  24. Msg = msg
  25. }, 5);
  26. }
  27. catch
  28. {
  29. }
  30. Serilog.Log.Information(msg);
  31. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  32. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Info, Msg = msg });
  33. }
  34. public static async Task Warning(string msg, Exception ex = null)
  35. {
  36. var pro = Process.GetCurrentProcess();
  37. try
  38. {
  39. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  40. {
  41. LogTime = DateTime.Now,
  42. LogType = EnumLogTypeDto.Info,
  43. Module = pro.ProcessName,
  44. Msg = msg
  45. }, 5);
  46. }
  47. catch
  48. {
  49. }
  50. Serilog.Log.Warning(ex, msg);
  51. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  52. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Warning, Msg = msg });
  53. }
  54. public static async Task Error(string msg, Exception ex = null)
  55. {
  56. if (ex != null && ex.GetType() == typeof(AggregateException))
  57. {
  58. var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault();
  59. if (iex != null)
  60. ex = iex;
  61. }
  62. var pro = Process.GetCurrentProcess();
  63. try
  64. {
  65. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  66. {
  67. LogTime = DateTime.Now,
  68. LogType = EnumLogTypeDto.Info,
  69. Module = pro.ProcessName,
  70. Msg = msg
  71. }, 5);
  72. }
  73. catch
  74. {
  75. }
  76. Serilog.Log.Error(ex, msg);
  77. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  78. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Error, Msg = msg });
  79. }
  80. }
  81. }