LogManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. namespace Aitex.Core.RT.Log
  10. {
  11. public class LogManager : ICommonLog
  12. {
  13. public const int MaxLogsMonth = 3;
  14. PeriodicJob _loggingJob;
  15. PeriodicJob _threadDeleteLogs;
  16. FixSizeQueue<LogItem> _logQueue;
  17. LogWriter _writer;
  18. public LogManager()
  19. {
  20. }
  21. public void Initialize()
  22. {
  23. _logQueue = new FixSizeQueue<LogItem>(1000);
  24. _loggingJob = new PeriodicJob(300, this.PeriodicRun, "Save Log Job", true);
  25. _threadDeleteLogs = new PeriodicJob(1000 * 60 * 60 * 24, OnDeleteLog, "Delete Log Thread", true);
  26. _writer = new LogWriter();
  27. LOG.InnerLogger = this;
  28. }
  29. public void Terminate()
  30. {
  31. try
  32. {
  33. if (_loggingJob != null)
  34. {
  35. _loggingJob.Stop();
  36. _loggingJob = null;
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. System.Diagnostics.Trace.WriteLine(ex.Message);
  42. }
  43. }
  44. public void Info(string message, bool isTraceOn)
  45. {
  46. CacheLog(message, Level.Info, null, isTraceOn);
  47. }
  48. public void Warning(string message)
  49. {
  50. CacheLog(message, Level.Warn, null, true);
  51. }
  52. public void Error(string message)
  53. {
  54. CacheLog(message, Level.Error, null, true);
  55. }
  56. public void Warning(string message, Exception ex)
  57. {
  58. CacheLog(message, Level.Warn, ex, true);
  59. }
  60. public void Error(string message, Exception ex)
  61. {
  62. CacheLog(message, Level.Error, ex, true);
  63. }
  64. bool PeriodicRun()
  65. {
  66. LogItem item;
  67. while (_logQueue.TryDequeue(out item))
  68. {
  69. string log = _writer.Write(item);
  70. }
  71. return true;
  72. }
  73. bool OnDeleteLog()
  74. {
  75. try
  76. {
  77. string path = string.Format(@"{0}", "Logs");
  78. FileInfo[] fileInfos;
  79. DirectoryInfo curFolderInfo = new DirectoryInfo(path);
  80. fileInfos = curFolderInfo.GetFiles();
  81. foreach (FileInfo info in fileInfos)
  82. {
  83. if (info.Name.Contains("appLog") || (info.Name.Contains("log")) && info.Extension == ".txt")
  84. {
  85. DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString());
  86. DateTime intervalTime = DateTime.Now.AddMonths(-MaxLogsMonth);// DateTime.Parse(DateTime.Now.AddMonths(-1).ToShortDateString());
  87. if (lastWriteTime < intervalTime)
  88. {
  89. File.Delete(info.FullName);
  90. //LOG.Write(string.Format("【log】自动删除log成功,logName:{0}", info.Name));
  91. }
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. LOG.Write(ex);
  98. }
  99. return true;
  100. }
  101. void CacheLog(string message, Level level, Exception exception, bool isTraceOn)
  102. {
  103. if (isTraceOn)
  104. {
  105. //System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
  106. }
  107. StackTrace st = new StackTrace(true);
  108. StackFrame sf = st.GetFrame(3);
  109. _logQueue.Enqueue(new LogItem(message, sf, level, exception));
  110. }
  111. }
  112. }