LogManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, string stackFile)
  45. {
  46. CacheLog(message, Level.Info, null, isTraceOn, stackFile);
  47. }
  48. public void Warning(string message)
  49. {
  50. CacheLog(message, Level.Warn, null, true, null);
  51. }
  52. public void Warning(string message, string stackFile)
  53. {
  54. CacheLog(message, Level.Warn, null, true, stackFile);
  55. }
  56. public void Error(string message, string stackFile)
  57. {
  58. CacheLog(message, Level.Error, null, true, stackFile);
  59. }
  60. public void Warning(string message, Exception ex, string stackFile)
  61. {
  62. CacheLog(message, Level.Warn, ex, true, stackFile);
  63. }
  64. public void Error(string message, Exception ex, string stackFile)
  65. {
  66. CacheLog(message, Level.Error, ex, true, stackFile);
  67. }
  68. bool PeriodicRun()
  69. {
  70. LogItem item;
  71. while (_logQueue.TryDequeue(out item))
  72. {
  73. string log = _writer.Write(item);
  74. }
  75. return true;
  76. }
  77. bool OnDeleteLog()
  78. {
  79. try
  80. {
  81. string path = string.Format(@"{0}", "Logs");
  82. FileInfo[] fileInfos;
  83. DirectoryInfo curFolderInfo = new DirectoryInfo(path);
  84. fileInfos = curFolderInfo.GetFiles();
  85. foreach (FileInfo info in fileInfos)
  86. {
  87. if (info.Name.Contains("log") && info.Extension == ".txt")
  88. {
  89. DateTime lastWriteTime = DateTime.Parse(info.LastWriteTime.ToShortDateString());
  90. DateTime intervalTime = DateTime.Now.AddMonths(-MaxLogsMonth);// DateTime.Parse(DateTime.Now.AddMonths(-1).ToShortDateString());
  91. if (lastWriteTime < intervalTime)
  92. {
  93. File.Delete(info.FullName);
  94. //LOG.Write(string.Format("【log】自动删除log成功,logName:{0}", info.Name));
  95. }
  96. }
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. LOG.Write(ex);
  102. }
  103. return true;
  104. }
  105. void CacheLog(string message, Level level, Exception exception, bool isTraceOn, string stackFile)
  106. {
  107. //if (isTraceOn)
  108. //{
  109. // System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
  110. //}
  111. _logQueue.Enqueue(new LogItem(message, new StackTrace(true).GetFrame(5), level, exception, stackFile));
  112. }
  113. }
  114. }