LogHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 X2LeoTaskServer54
  9. {
  10. public static class LogHelper
  11. {
  12. public static string BaseUrl;
  13. public static async Task Info(string msg)
  14. {
  15. var pro = Process.GetCurrentProcess();
  16. try
  17. {
  18. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  19. {
  20. LogTime = DateTime.Now,
  21. LogType = EnumLogTypeDto.Info,
  22. Module = pro.ProcessName,
  23. Msg = msg
  24. }, 5);
  25. }
  26. catch
  27. {
  28. }
  29. Serilog.Log.Information(msg);
  30. }
  31. public static async Task Warning(string msg, Exception ex = null)
  32. {
  33. var pro = Process.GetCurrentProcess();
  34. try
  35. {
  36. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  37. {
  38. LogTime = DateTime.Now,
  39. LogType = EnumLogTypeDto.Warning,
  40. Module = pro.ProcessName,
  41. Msg = msg
  42. }, 5);
  43. }
  44. catch
  45. {
  46. }
  47. Serilog.Log.Warning(ex, msg);
  48. }
  49. public static async Task Error(string msg, Exception ex = null)
  50. {
  51. if (ex != null && ex.GetType() == typeof(AggregateException))
  52. {
  53. var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault();
  54. if (iex != null)
  55. ex = iex;
  56. }
  57. var pro = Process.GetCurrentProcess();
  58. try
  59. {
  60. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  61. {
  62. LogTime = DateTime.Now,
  63. LogType = EnumLogTypeDto.Error,
  64. Module = pro.ProcessName,
  65. Msg = msg
  66. }, 5);
  67. }
  68. catch
  69. {
  70. }
  71. Serilog.Log.Error(ex, msg);
  72. }
  73. }
  74. }