LogManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. private TypedLogWriter _logger;
  32. public void Initialize(string Type)
  33. {
  34. _logger = new TypedLogWriter(Type);
  35. //_threadWphLogs = new PeriodicJob(1000, OnWphLog, "WphLog", true);
  36. }
  37. public bool LogWhplog(string textlog)
  38. {
  39. try
  40. {
  41. if(_logger !=null)
  42. _logger.Write(textlog);
  43. }
  44. catch(Exception ex)
  45. {
  46. LOG.Write(ex);
  47. }
  48. return true;
  49. }
  50. //public bool OnWphLog()
  51. //{
  52. // try
  53. // {
  54. // if (DateTime.Now.Hour < 23)
  55. // {
  56. // return true;
  57. // }
  58. // if (DateTime.Now.Minute < 59)
  59. // {
  60. // return true;
  61. // }
  62. // if (DateTime.Now.Second < 58)
  63. // {
  64. // return true;
  65. // }
  66. // int RunTime = SC.GetValue<int>("System.SystemAutoRunTime");
  67. // int IdleTime = SC.GetValue<int>("System.SystemIdleTime");
  68. // int AlarmTime = SC.GetValue<int>("System.SystemAlarmTime");
  69. // int StrWph = SC.GetValue<int>("System.DayThroughput");
  70. // int TimeNow = DateTime.Now.Second + (DateTime.Now.Hour * 60 + DateTime.Now.Minute) * 60;
  71. // int StrUptime = (TimeNow - AlarmTime) * 100 / TimeNow;
  72. // //int StrUtility = RunTime * 100 / (RunTime + IdleTime);
  73. // int StrUtility = RunTime * 100 / TimeNow;
  74. // _logger.Write($"System Uptime is {StrUptime}%, Throughput is {StrWph},Utility is {StrUtility}%");
  75. // }
  76. // catch (Exception ex)
  77. // {
  78. // LOG.Write(ex);
  79. // }
  80. // return true;
  81. //}
  82. public void Terminate()
  83. {
  84. try
  85. {
  86. if (_loggingJob != null)
  87. {
  88. _loggingJob.Stop();
  89. _loggingJob = null;
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. System.Diagnostics.Trace.WriteLine(ex.Message);
  95. }
  96. }
  97. public void Info(string message, bool isTraceOn, string stackFile)
  98. {
  99. CacheLog(message, Level.Info, null, isTraceOn, stackFile);
  100. }
  101. public void Warning(string message, string stackFile)
  102. {
  103. CacheLog(message, Level.Warn, null, true, stackFile);
  104. }
  105. public void Error(string message, string stackFile)
  106. {
  107. CacheLog(message, Level.Error, null, true, stackFile);
  108. }
  109. public void Warning(string message, Exception ex, string stackFile)
  110. {
  111. CacheLog(message, Level.Warn, ex, true, stackFile);
  112. }
  113. public void Error(string message, Exception ex, string stackFile)
  114. {
  115. CacheLog(message, Level.Error, ex, true, stackFile);
  116. }
  117. bool PeriodicRun()
  118. {
  119. LogItem item;
  120. while (_logQueue.TryDequeue(out item))
  121. {
  122. string log = _writer.Write(item);
  123. }
  124. return true;
  125. }
  126. void CacheLog(string message, Level level, Exception exception, bool isTraceOn, string stackFile)
  127. {
  128. //if (isTraceOn)
  129. //{
  130. // System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
  131. //}
  132. _logQueue.Enqueue(new LogItem(message, new StackTrace(true).GetFrame(5), level, exception, stackFile));
  133. }
  134. }
  135. }