LogManager.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using log4net.Core;
  6. using System.Diagnostics;
  7. using Aitex.DataAnalysis.Core;
  8. namespace Aitex.DataAnalysis.Log
  9. {
  10. public class LogManager : ICommonLog
  11. {
  12. PeriodicJob _loggingJob;
  13. FixSizeQueue<LogItem> _logQueue;
  14. LogWriter _writer;
  15. public LogManager()
  16. {
  17. _logQueue = new FixSizeQueue<LogItem>(1000);
  18. _loggingJob = new PeriodicJob(200, this.PeriodicRun, "Save Log Job", true);
  19. _writer = new LogWriter();
  20. LOG.InnerLogger = this;
  21. }
  22. public void Terminate()
  23. {
  24. if (_loggingJob != null)
  25. {
  26. _loggingJob.Stop();
  27. _loggingJob = null;
  28. }
  29. }
  30. public void Info(string message, bool isTraceOn=true)
  31. {
  32. CacheLog(message, Level.Info, null, isTraceOn);
  33. }
  34. public void Warning(string message)
  35. {
  36. CacheLog(message, Level.Warn, null, true);
  37. }
  38. public void Error(string message)
  39. {
  40. CacheLog(message, Level.Error, null, true);
  41. }
  42. public void Warning(string message, Exception ex)
  43. {
  44. CacheLog(message, Level.Warn, ex, true);
  45. }
  46. public void Error(string message, Exception ex)
  47. {
  48. CacheLog(message, Level.Error, ex, true);
  49. }
  50. bool PeriodicRun()
  51. {
  52. LogItem item;
  53. while (_logQueue.TryDequeue(out item))
  54. {
  55. string log = _writer.Write(item);
  56. }
  57. return true;
  58. }
  59. void CacheLog(string message, Level level, Exception exception, bool isTraceOn)
  60. {
  61. if (isTraceOn)
  62. {
  63. System.Diagnostics.Trace.WriteLine(message + (exception == null ? "" : exception.Message));
  64. }
  65. _logQueue.Enqueue(new LogItem(message, new StackTrace(true).GetFrame(3), level, exception));
  66. }
  67. }
  68. }