LogHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using XdCxRhDW.Dto;
  8. namespace X2D1TaskServer
  9. {
  10. public class LogInfo
  11. {
  12. public LogInfo()
  13. {
  14. this.LogTime = DateTime.Now;
  15. this.LogType = EnumLogType.Info;
  16. }
  17. [Display(Name = "日志类型")]
  18. public EnumLogType LogType { get; set; }
  19. [Display(Name = "时间")]
  20. public DateTime LogTime { get; private set; }
  21. [Display(Name = "内容")]
  22. public string Msg { get; set; }
  23. }
  24. public enum EnumLogType
  25. {
  26. [Display(Name = "消息")]
  27. Info,
  28. [Display(Name = "警告")]
  29. Warning,
  30. [Display(Name = "错误")]
  31. Error
  32. }
  33. public static class LogHelper
  34. {
  35. public static string BaseUrl;
  36. public static Action<LogInfo> Logger;
  37. public static async Task Info(string msg)
  38. {
  39. var pro = Process.GetCurrentProcess();
  40. try
  41. {
  42. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  43. {
  44. LogTime = DateTime.Now,
  45. LogType = EnumLogTypeDto.Info,
  46. Module = pro.ProcessName,
  47. Msg = msg
  48. }, 5);
  49. }
  50. catch
  51. {
  52. }
  53. Serilog.Log.Information(msg);
  54. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  55. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Info, Msg = msg });
  56. }
  57. public static async Task Warning(string msg, Exception ex = null)
  58. {
  59. var pro = Process.GetCurrentProcess();
  60. try
  61. {
  62. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  63. {
  64. LogTime = DateTime.Now,
  65. LogType = EnumLogTypeDto.Warning,
  66. Module = pro.ProcessName,
  67. Msg = msg
  68. }, 5);
  69. }
  70. catch
  71. {
  72. }
  73. Serilog.Log.Warning(ex, msg);
  74. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  75. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Warning, Msg = msg });
  76. }
  77. public static async Task Error(string msg, Exception ex = null)
  78. {
  79. if (ex != null && ex.GetType() == typeof(AggregateException))
  80. {
  81. var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault();
  82. if (iex != null)
  83. ex = iex;
  84. }
  85. var pro = Process.GetCurrentProcess();
  86. try
  87. {
  88. await HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  89. {
  90. LogTime = DateTime.Now,
  91. LogType = EnumLogTypeDto.Error,
  92. Module = pro.ProcessName,
  93. Msg = msg
  94. }, 5);
  95. }
  96. catch
  97. {
  98. }
  99. Serilog.Log.Error(ex, msg);
  100. if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle))
  101. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Error, Msg = msg });
  102. }
  103. }
  104. }