LogHelper.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 void Info(string msg)
  15. {
  16. var pro = Process.GetCurrentProcess();
  17. _ = HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  18. {
  19. LogTime = DateTime.Now,
  20. LogType = EnumLogTypeDto.Info,
  21. Module = pro.ProcessName,
  22. Msg = msg
  23. });
  24. Serilog.Log.Information(msg);
  25. Serilog.Log.Information("MainWindowTitle=" + pro.MainWindowTitle);
  26. Serilog.Log.Information("ModuleName=" + pro.MainModule.ModuleName);
  27. Serilog.Log.Information("FileName=" + pro.MainModule.FileName);
  28. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Info, Msg = msg });
  29. }
  30. public static void Warning(string msg, Exception ex = null)
  31. {
  32. _ = HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  33. {
  34. LogTime = DateTime.Now,
  35. LogType = EnumLogTypeDto.Warning,
  36. Module = Process.GetCurrentProcess().ProcessName,
  37. Msg = msg
  38. });
  39. Serilog.Log.Warning(ex, msg);
  40. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Warning, Msg = msg });
  41. }
  42. public static void Error(string msg, Exception ex = null)
  43. {
  44. if (ex != null && ex.GetType() == typeof(AggregateException))
  45. {
  46. var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault();
  47. if (iex != null)
  48. ex = iex;
  49. }
  50. _ = HttpHelper.PostRequestAsync<List<LogModulesResDto>>(BaseUrl + "Log/Add", new LogInfoDto()
  51. {
  52. LogTime = DateTime.Now,
  53. LogType = EnumLogTypeDto.Error,
  54. Module = Process.GetCurrentProcess().ProcessName,
  55. Msg = msg
  56. });
  57. Serilog.Log.Error(ex, msg);
  58. Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Error, Msg = msg });
  59. }
  60. }
  61. }