LogManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Util;
  6. using log4net.Core;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using Aitex.Common.Util;
  10. using Aitex.Core.RT.SCCore;
  11. namespace Aitex.Core.RT.Log
  12. {
  13. public class LogManager : ICommonLog
  14. {
  15. PeriodicJob _threadWphLogs;
  16. PeriodicJob _loggingJob;
  17. FixSizeQueue<LogItem> _logQueue;
  18. LogWriter _writer;
  19. public LogManager()
  20. {
  21. }
  22. public void Initialize()
  23. {
  24. _logQueue = new FixSizeQueue<LogItem>(1000);
  25. _loggingJob = new PeriodicJob(300, this.PeriodicRun, "Save Log Job", true);
  26. _writer = new LogWriter();
  27. LOG.InnerLogger = this;
  28. //string s1 = System.Diagnostics.FileVersionInfo.GetVersionInfo(Path.Combine(PathManager.GetAppDir(), "EfemDualRT.exe")).ProductVersion;
  29. //LOG.Write($"RT {s1} launch ...");
  30. }
  31. public void InitLogDirectory()
  32. {
  33. _writer.SetLogDirectory();
  34. }
  35. private TypedLogWriter _logger;
  36. public void Initialize(string Type)
  37. {
  38. _logger = new TypedLogWriter(Type);
  39. _threadWphLogs = new PeriodicJob(1000, OnWphLog, "WphLog", true);
  40. }
  41. bool OnWphLog()
  42. {
  43. try
  44. {
  45. if (DateTime.Now.Hour < 23)
  46. {
  47. return true;
  48. }
  49. if (DateTime.Now.Minute < 59)
  50. {
  51. return true;
  52. }
  53. if (DateTime.Now.Second < 58)
  54. {
  55. return true;
  56. }
  57. int RunTime = SC.GetValue<int>("System.SystemAutoRunTime");
  58. int IdleTime = SC.GetValue<int>("System.SystemIdleTime");
  59. int AlarmTime = SC.GetValue<int>("System.SystemAlarmTime");
  60. int StrWph = SC.GetValue<int>("System.DayThroughput");
  61. int TimeNow = DateTime.Now.Second + (DateTime.Now.Hour * 60 + DateTime.Now.Minute) * 60;
  62. int StrUptime = (TimeNow - AlarmTime) * 100 / TimeNow;
  63. //int StrUtility = RunTime * 100 / (RunTime + IdleTime);
  64. int StrUtility = RunTime * 100 / TimeNow;
  65. _logger.Write($"System Uptime is {StrUptime}%, Throughput is {StrWph},Utility is {StrUtility}%");
  66. }
  67. catch (Exception ex)
  68. {
  69. LOG.Write(ex);
  70. }
  71. return true;
  72. }
  73. public void Terminate()
  74. {
  75. try
  76. {
  77. if (_loggingJob != null)
  78. {
  79. _loggingJob.Stop();
  80. _loggingJob = null;
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. System.Diagnostics.Trace.WriteLine(ex.Message);
  86. }
  87. }
  88. public void Info(string message, bool isTraceOn, string stackFile)
  89. {
  90. CacheLog(message, Level.Info, null, isTraceOn, stackFile);
  91. }
  92. public void Warning(string message, string stackFile)
  93. {
  94. CacheLog(message, Level.Warn, null, true, stackFile);
  95. }
  96. public void Error(string message, string stackFile)
  97. {
  98. CacheLog(message, Level.Error, null, true, stackFile);
  99. }
  100. public void Warning(string message, Exception ex, string stackFile)
  101. {
  102. CacheLog(message, Level.Warn, ex, true, stackFile);
  103. }
  104. public void Error(string message, Exception ex, string stackFile)
  105. {
  106. CacheLog(message, Level.Error, ex, true, stackFile);
  107. }
  108. bool PeriodicRun()
  109. {
  110. LogItem item;
  111. while (_logQueue.TryDequeue(out item))
  112. {
  113. string log = _writer.Write(item);
  114. }
  115. return true;
  116. }
  117. void CacheLog(string message, Level level, Exception exception, bool isTraceOn, string stackFile)
  118. {
  119. //if (isTraceOn)
  120. //{
  121. // System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
  122. //}
  123. _logQueue.Enqueue(new LogItem(message, new StackTrace(true).GetFrame(5), level, exception, stackFile));
  124. }
  125. }
  126. }