LOG.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Aitex.Core.RT.SCCore;
  2. using System;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using MECF.Framework.Common.Equipment;
  6. using System.Text;
  7. using Aitex.Core.RT.Event;
  8. namespace Aitex.Core.RT.Log
  9. {
  10. public static class LOG
  11. {
  12. public enum Level
  13. {
  14. Undefine,
  15. Debug,
  16. Info,
  17. Warning,
  18. Error
  19. }
  20. public static ICommonLog InnerLogger { set; private get; }
  21. public static Action<ModuleName> PMErrorInterrupt;
  22. public static void onErrorInterrupt(ModuleName module)
  23. {
  24. if (PMErrorInterrupt != null && ModuleHelper.IsPm(module))
  25. PMErrorInterrupt(module);
  26. }
  27. private static void Debug(string message)
  28. {
  29. if (InnerLogger != null && SC.GetValue<bool>("System.IsOpenDebugLog") ==true)
  30. InnerLogger.Debug(message);
  31. }
  32. private static void Info(string message)
  33. {
  34. if (InnerLogger != null)
  35. InnerLogger.Info(message);
  36. }
  37. private static void Warning(string message)
  38. {
  39. if (InnerLogger != null)
  40. InnerLogger.Warning(message);
  41. }
  42. private static void Error(string message)
  43. {
  44. if (InnerLogger != null)
  45. InnerLogger.Error(message);
  46. }
  47. static string GetFormatStackFrameInfo(Exception ex, int traceLevel = 2)
  48. {
  49. StackFrame sf = new StackTrace(true).GetFrame(traceLevel + 1);
  50. string pathFile = sf.GetFileName();
  51. string file = string.IsNullOrEmpty(pathFile) ? "" : pathFile.Substring(pathFile.LastIndexOf('\\') + 1);
  52. return $"{ex.Message}\r\n{file}\tLine {sf.GetFileLineNumber()}\t{sf.GetMethod().Name}()";
  53. }
  54. public static void Write(eEvent id, string module, params string[] values)
  55. {
  56. Write(id, ModuleHelper.Converter(module), values);
  57. }
  58. public static void WriteExeption(Exception ex)
  59. {
  60. Write(eEvent.ERR_EXCEPTION, ModuleName.System, GetFormatStackFrameInfo(ex));
  61. }
  62. public static void WriteExeption(string prefix, Exception ex)
  63. {
  64. Write(eEvent.ERR_EXCEPTION, ModuleName.System, prefix + GetFormatStackFrameInfo(ex));
  65. }
  66. public static void WriteSingeLine(eEvent id, ModuleName module, string log)
  67. {
  68. string newLog = log.Replace("\r", "<回车>");
  69. newLog = newLog.Replace("\n", "<换行>");
  70. Write(id, module, newLog);
  71. }
  72. public static void Write(eEvent id, ModuleName module, params string[] values)
  73. {
  74. //U can Add here to postmsg(MSG.Error) to target entity by the func
  75. //if (id == eEvent.ERR_DEVICE_INFO)
  76. // onErrorInterrupt(module);
  77. var logItem= LogDefineManager.LogItems?.Where(x => x.Id == id).FirstOrDefault();
  78. if (logItem != null)
  79. {
  80. var item1 = logItem.Id;
  81. var item2 = string.Format(logItem.GlobalDescription_zh, values);
  82. var item3 = module.ToString();
  83. StringBuilder text = new StringBuilder();
  84. text.Append(((int)item1).ToString().PadLeft(8));
  85. text.Append(item3.PadLeft(12));
  86. text.Append(" ");
  87. text.Append(string.Format(logItem.GlobalDescription_zh, values));
  88. string message = text.ToString();
  89. //test += (message + "\r\n");
  90. Level level = Level.Undefine;
  91. Enum.TryParse<Level>(logItem.Level, out level);
  92. switch (level)
  93. {
  94. case Level.Debug:
  95. Debug(message);
  96. break;
  97. case Level.Info:
  98. //EV.PostInfoLog(module.ToString(), id, string.Format(((int)item1).ToString().PadRight(6)+ logItem.GlobalDescription_zh, values));
  99. EV.PostInfoLog(module.ToString(), id, string.Format(logItem.GlobalDescription_zh, values));
  100. Info(message);
  101. break;
  102. case Level.Warning:
  103. EV.PostWarningLog(module.ToString(),id, string.Format(logItem.GlobalDescription_zh, values));
  104. Warning(message);
  105. break;
  106. case Level.Error:
  107. EV.PostAlarmLog(module.ToString(),id, string.Format(logItem.GlobalDescription_zh, values));
  108. Error(message);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }