LOG.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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,eEvent> PMErrorInterrupt;
  24. private static DateTime _lstErrTime;
  25. private static Queue<string> _errMessages = new Queue<string>();
  26. public static void onErrorInterrupt(ModuleName module,eEvent eEventid)
  27. {
  28. if (PMErrorInterrupt != null && ModuleHelper.IsPm(module))
  29. PMErrorInterrupt(module,eEventid);
  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. if (logItem == null)
  100. {
  101. return;
  102. }
  103. string logWithoutControlChar="";
  104. foreach (char logchar in string.Format(logItem.GlobalDescription_zh, values))
  105. {
  106. if (char.IsControl(logchar))
  107. {
  108. if(logchar == 9)
  109. logWithoutControlChar += logchar;
  110. else
  111. logWithoutControlChar += $"[{Convert.ToString(logchar,16)}]";
  112. }
  113. else if (logchar == 127)
  114. {
  115. logWithoutControlChar += "[DEL]";
  116. }
  117. else
  118. {
  119. logWithoutControlChar += logchar;
  120. }
  121. }
  122. if (logItem != null)
  123. {
  124. var item1 = logItem.Id;
  125. var item2 = logWithoutControlChar;
  126. var item3 = module.ToString();
  127. StringBuilder text = new StringBuilder();
  128. text.Append(((int)item1).ToString().PadLeft(8));
  129. text.Append(item3.PadLeft(12));
  130. text.Append(" ");
  131. text.Append(logWithoutControlChar);
  132. string message = text.ToString();
  133. //test += (message + "\r\n");
  134. Level level = Level.Undefine;
  135. Enum.TryParse<Level>(logItem.Level, out level);
  136. switch (level)
  137. {
  138. case Level.Debug:
  139. Debug(message);
  140. break;
  141. case Level.Info:
  142. EV.PostInfoLog(module.ToString(), id, logWithoutControlChar);
  143. Info(message);
  144. break;
  145. case Level.Warning:
  146. EV.PostWarningLog(module.ToString(),id, logWithoutControlChar);
  147. Warning(message);
  148. break;
  149. case Level.Error:
  150. EV.PostAlarmLog(module.ToString(),id, logWithoutControlChar);
  151. Error(message);
  152. onErrorInterrupt(module,id);
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. }