LOG.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using System.Collections.Generic;
  9. using System.Runtime.InteropServices.ComTypes;
  10. namespace Aitex.Core.RT.Log
  11. {
  12. public static class LOG
  13. {
  14. public enum Level
  15. {
  16. Undefine,
  17. Debug,
  18. Info,
  19. Warning,
  20. Error
  21. }
  22. public static ICommonLog InnerLogger { set; private get; }
  23. public static Action<ModuleName> PMErrorInterrupt;
  24. private static DateTime _lstErrTime;
  25. private static Queue<string> _errMessages = new Queue<string>();
  26. public static void onErrorInterrupt(ModuleName module)
  27. {
  28. if (PMErrorInterrupt != null && ModuleHelper.IsPm(module))
  29. PMErrorInterrupt(module);
  30. }
  31. private static void Debug(string message)
  32. {
  33. if (InnerLogger != null && SC.GetValue<bool>("System.IsOpenDebugLog") ==true)
  34. InnerLogger.Debug(message);
  35. }
  36. public static void Info(string message)
  37. {
  38. if (InnerLogger != null)
  39. InnerLogger.Info(message);
  40. }
  41. public static void Warning(string message)
  42. {
  43. if (InnerLogger != null)
  44. InnerLogger.Warning(message);
  45. }
  46. public static void Error(string message)
  47. {
  48. if (InnerLogger != null)
  49. InnerLogger.Error(message);
  50. }
  51. static string GetFormatStackFrameInfo(Exception ex, int traceLevel = 2)
  52. {
  53. StackFrame sf = new StackTrace(true).GetFrame(traceLevel + 1);
  54. string pathFile = sf.GetFileName();
  55. string file = string.IsNullOrEmpty(pathFile) ? "" : pathFile.Substring(pathFile.LastIndexOf('\\') + 1);
  56. return $"{ex.Message}\r\n{file}\tLine {sf.GetFileLineNumber()}\t{sf.GetMethod().Name}()";
  57. }
  58. public static void Write(eEvent id, string module, params string[] values)
  59. {
  60. Write(id, ModuleHelper.Converter(module), values);
  61. }
  62. public static void WriteExeption(Exception ex)
  63. {
  64. if (!_errMessages.Contains(ex.Message))
  65. {
  66. if (_errMessages.Count > 30)
  67. {
  68. _errMessages.Dequeue();
  69. _errMessages.Enqueue(ex.Message);
  70. }
  71. else
  72. _errMessages.Enqueue(ex.Message);
  73. _lstErrTime = DateTime.Now;
  74. Write(eEvent.ERR_EXCEPTION, ModuleName.System, GetFormatStackFrameInfo(ex));
  75. }
  76. else if (_lstErrTime == null || (DateTime.Now - _lstErrTime).TotalMilliseconds > 10000)
  77. {
  78. _lstErrTime = DateTime.Now;
  79. Write(eEvent.ERR_EXCEPTION, ModuleName.System, GetFormatStackFrameInfo(ex));
  80. }
  81. }
  82. public static void WriteExeption(string prefix, Exception ex)
  83. {
  84. Write(eEvent.ERR_EXCEPTION, ModuleName.System, prefix + GetFormatStackFrameInfo(ex));
  85. }
  86. public static void WriteSingeLine(eEvent id, ModuleName module, string log)
  87. {
  88. string newLog = log.Replace("\r", "<回车>");
  89. newLog = newLog.Replace("\n", "<换行>");
  90. Write(id, module, newLog);
  91. }
  92. public static void Write(eEvent id, ModuleName module, params string[] values)
  93. {
  94. //U can Add here to postmsg(MSG.Error) to target entity by the func
  95. //if (id == eEvent.ERR_DEVICE_INFO)
  96. // onErrorInterrupt(module);
  97. var logItem= LogDefineManager.LogItems?.Where(x => x.Id == id).FirstOrDefault();
  98. // transform the Control Char to HEX number
  99. string logWithoutControlChar="";
  100. foreach (char logchar in string.Format(logItem.GlobalDescription_zh, values))
  101. {
  102. if (char.IsControl(logchar))
  103. {
  104. if(logchar == 9)
  105. logWithoutControlChar += logchar;
  106. else
  107. logWithoutControlChar += $"[{Convert.ToString(logchar,16)}]";
  108. }
  109. else if (logchar == 127)
  110. {
  111. logWithoutControlChar += "[DEL]";
  112. }
  113. else
  114. {
  115. logWithoutControlChar += logchar;
  116. }
  117. }
  118. if (logItem != null)
  119. {
  120. var item1 = logItem.Id;
  121. var item2 = logWithoutControlChar;
  122. var item3 = module.ToString();
  123. StringBuilder text = new StringBuilder();
  124. text.Append(((int)item1).ToString().PadLeft(8));
  125. text.Append(item3.PadLeft(12));
  126. text.Append(" ");
  127. text.Append(logWithoutControlChar);
  128. string message = text.ToString();
  129. //test += (message + "\r\n");
  130. Level level = Level.Undefine;
  131. Enum.TryParse<Level>(logItem.Level, out level);
  132. switch (level)
  133. {
  134. case Level.Debug:
  135. Debug(message);
  136. break;
  137. case Level.Info:
  138. //EV.PostInfoLog(module.ToString(), id, string.Format(((int)item1).ToString().PadRight(6)+ logItem.GlobalDescription_zh, values));
  139. EV.PostInfoLog(module.ToString(), id, logWithoutControlChar);
  140. Info(message);
  141. break;
  142. case Level.Warning:
  143. EV.PostWarningLog(module.ToString(),id, logWithoutControlChar);
  144. Warning(message);
  145. break;
  146. case Level.Error:
  147. EV.PostAlarmLog(module.ToString(),id, logWithoutControlChar);
  148. Error(message);
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. }