LOG.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. private static void Debug(string message)
  22. {
  23. if (InnerLogger != null && SC.GetValue<bool>("System.IsOpenDebugLog") ==true)
  24. InnerLogger.Debug(message);
  25. }
  26. private static void Info(string message)
  27. {
  28. if (InnerLogger != null)
  29. InnerLogger.Info(message);
  30. }
  31. private static void Warning(string message)
  32. {
  33. if (InnerLogger != null)
  34. InnerLogger.Warning(message);
  35. }
  36. private static void Error(string message)
  37. {
  38. if (InnerLogger != null)
  39. InnerLogger.Error(message);
  40. }
  41. static string GetFormatStackFrameInfo(Exception ex, int traceLevel = 2)
  42. {
  43. StackFrame sf = new StackTrace(true).GetFrame(traceLevel + 1);
  44. string pathFile = sf.GetFileName();
  45. string file = string.IsNullOrEmpty(pathFile) ? "" : pathFile.Substring(pathFile.LastIndexOf('\\') + 1);
  46. return $"{ex.Message}\r\n{file}\tLine {sf.GetFileLineNumber()}\t{sf.GetMethod().Name}()";
  47. }
  48. public static void Write(eEvent id, string module, params string[] values)
  49. {
  50. Write(id, ModuleHelper.Converter(module), values);
  51. }
  52. public static void WriteExeption(Exception ex)
  53. {
  54. Write(eEvent.ERR_EXCEPTION, ModuleName.System, GetFormatStackFrameInfo(ex));
  55. }
  56. public static void WriteExeption(string prefix, Exception ex)
  57. {
  58. Write(eEvent.ERR_EXCEPTION, ModuleName.System, prefix + GetFormatStackFrameInfo(ex));
  59. }
  60. public static void WriteSingeLine(eEvent id, ModuleName module, string log)
  61. {
  62. string newLog = log.Replace("\r", "<回车>");
  63. newLog = newLog.Replace("\n", "<换行>");
  64. Write(id, module, newLog);
  65. }
  66. public static void Write(eEvent id, ModuleName module, params string[] values)
  67. {
  68. var logItem= LogDefineManager.LogItems?.Where(x => x.Id == id).FirstOrDefault();
  69. if (logItem != null)
  70. {
  71. var item1 = logItem.Id;
  72. var item2 = string.Format(logItem.GlobalDescription_zh, values);
  73. var item3 = module.ToString();
  74. StringBuilder text = new StringBuilder();
  75. text.Append(((int)item1).ToString().PadLeft(8));
  76. text.Append(item3.PadLeft(12));
  77. text.Append(" ");
  78. text.Append(string.Format(logItem.GlobalDescription_zh, values));
  79. string message = text.ToString();
  80. //test += (message + "\r\n");
  81. Level level = Level.Undefine;
  82. Enum.TryParse<Level>(logItem.Level, out level);
  83. switch (level)
  84. {
  85. case Level.Debug:
  86. Debug(message);
  87. break;
  88. case Level.Info:
  89. //EV.PostInfoLog(module.ToString(), id, string.Format(((int)item1).ToString().PadRight(6)+ logItem.GlobalDescription_zh, values));
  90. EV.PostInfoLog(module.ToString(), id, string.Format(logItem.GlobalDescription_zh, values));
  91. Info(message);
  92. break;
  93. case Level.Warning:
  94. EV.PostWarningLog(module.ToString(),id, string.Format(logItem.GlobalDescription_zh, values));
  95. Warning(message);
  96. break;
  97. case Level.Error:
  98. EV.PostAlarmLog(module.ToString(),id, string.Format(logItem.GlobalDescription_zh, values));
  99. Error(message);
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. }