LogUI.cs 3.4 KB

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