LogHelper.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. using XdCxRhDW.Entity;
  9. using XdCxRhDW.Repostory;
  10. namespace XdCxRhDW.App
  11. {
  12. public static class LogHelper
  13. {
  14. public static async Task Info(string msg)
  15. {
  16. var pro = Process.GetCurrentProcess();
  17. try
  18. {
  19. using (MySqlContext db = new MySqlContext())
  20. {
  21. LogRes res = new LogRes()
  22. {
  23. LogTime = DateTime.Now,
  24. Module = pro.ProcessName,
  25. LogType = EnumLogType.Info,
  26. Msg = msg,
  27. };
  28. db.LogRes.Add(res);
  29. await db.SaveChangesAsync();
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. XdCxRhDW.Framework.LogHelper.Error("日志信息写入异常", ex);
  35. }
  36. XdCxRhDW.Framework.LogHelper.Info(msg);
  37. }
  38. public static async Task Warning(string msg, Exception ex = null)
  39. {
  40. var pro = Process.GetCurrentProcess();
  41. try
  42. {
  43. using (MySqlContext db = new MySqlContext())
  44. {
  45. LogRes res = new LogRes()
  46. {
  47. LogTime = DateTime.Now,
  48. Module = pro.ProcessName,
  49. LogType = EnumLogType.Warning,
  50. Msg = msg,
  51. };
  52. db.LogRes.Add(res);
  53. await db.SaveChangesAsync();
  54. }
  55. }
  56. catch (Exception arg)
  57. {
  58. XdCxRhDW.Framework.LogHelper.Error("日志信息写入异常", arg);
  59. }
  60. XdCxRhDW.Framework.LogHelper.Warning(msg, ex);
  61. }
  62. public static async Task Error(string msg, Exception ex = null)
  63. {
  64. if (ex != null && ex.GetType() == typeof(AggregateException))
  65. {
  66. var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault();
  67. if (iex != null)
  68. ex = iex;
  69. }
  70. var pro = Process.GetCurrentProcess();
  71. try
  72. {
  73. using (MySqlContext db = new MySqlContext())
  74. {
  75. LogRes res = new LogRes()
  76. {
  77. LogTime = DateTime.Now,
  78. Module = pro.ProcessName,
  79. LogType = EnumLogType.Error,
  80. Msg = msg,
  81. };
  82. db.LogRes.Add(res);
  83. await db.SaveChangesAsync();
  84. }
  85. }
  86. catch (Exception arg)
  87. {
  88. XdCxRhDW.Framework.LogHelper.Error("日志信息写入异常", arg);
  89. }
  90. XdCxRhDW.Framework.LogHelper.Error(msg, ex);
  91. }
  92. }
  93. }