Logger.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Log;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using Universal;
  5. namespace Logger;
  6. public class Logger : ILog
  7. {
  8. private readonly ManualResetEvent _exitWait = new(false);
  9. private readonly ManualResetEvent _quitEvent = new(false);
  10. private EventQueue<LogInfo>? _logEventQueue;
  11. private FileStream? _logStream;
  12. private StreamWriter? _logWriter;
  13. private string _name = "UnInitialized";
  14. public string? Directory { get; internal set; }
  15. public string? FilePath { get; internal set; }
  16. bool ILog.Initialize(string fileName)
  17. {
  18. if (_logEventQueue is not null)
  19. return false;
  20. _logEventQueue = new(LogHandler);
  21. if (string.IsNullOrEmpty(fileName))
  22. fileName = "UnInitialized";
  23. string[] temp = fileName.Split('.');
  24. if (temp.Length > 1)
  25. fileName = temp[0];
  26. this._name = fileName;
  27. this.FilePath = Path.Combine(Environment.CurrentDirectory, "Logger", $"{fileName}_{DateTime.Now:MMdd_HH_mm_ss}.log");
  28. this.Directory = Path.GetDirectoryName(this.FilePath);
  29. if (string.IsNullOrEmpty(this.Directory))
  30. return false;
  31. if (!System.IO.Directory.Exists(this.Directory))
  32. System.IO.Directory.CreateDirectory(this.Directory);
  33. try
  34. {
  35. _logStream = new(this.FilePath, FileMode.OpenOrCreate);
  36. _logWriter = new(_logStream);
  37. _logWriter.WriteLine($"Log Start on {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
  38. _logWriter.Flush();
  39. }
  40. catch
  41. {
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool ILog.Error(string message, DateTime? dateTime)
  47. {
  48. if (_logEventQueue is null)
  49. ((ILog)this).Initialize(string.Empty);
  50. this._logEventQueue?.Enqueue(new(LogLevel.Error, message, dateTime));
  51. return true;
  52. }
  53. bool ILog.Fatal(string message, DateTime? dateTime)
  54. {
  55. if (_logEventQueue is null)
  56. ((ILog)this).Initialize(string.Empty);
  57. this._logEventQueue?.Enqueue(new(LogLevel.Fatal, message, dateTime));
  58. return true;
  59. }
  60. bool ILog.Info(string message, DateTime? dateTime)
  61. {
  62. if (_logEventQueue is null)
  63. ((ILog)this).Initialize(string.Empty);
  64. this._logEventQueue?.Enqueue(new(LogLevel.Info, message, dateTime));
  65. return true;
  66. }
  67. bool ILog.Warning(string message, DateTime? dateTime)
  68. {
  69. if (_logEventQueue is null)
  70. ((ILog)this).Initialize(string.Empty);
  71. this._logEventQueue?.Enqueue(new(LogLevel.Warning, message, dateTime));
  72. return true;
  73. }
  74. bool ILog.Debug(string message, DateTime? dateTime)
  75. {
  76. if (_logEventQueue is null)
  77. ((ILog)this).Initialize(string.Empty);
  78. this._logEventQueue?.Enqueue(new(LogLevel.Debug, message, dateTime));
  79. return true;
  80. }
  81. void LogHandler(LogInfo logInfo)
  82. {
  83. StringBuilder sb = new();
  84. sb.Append(logInfo.Time.ToString("HH:mm:ss fff\t"));
  85. sb.Append(logInfo.Level switch
  86. {
  87. LogLevel.Debug => $"Debug\t\t",
  88. LogLevel.Info => "\t\t\t",
  89. LogLevel.Warning => "!!!\t\t\t",
  90. LogLevel.Error => "xxx\t\t\t",
  91. LogLevel.Fatal => "XXXXXX\t\t",
  92. _ => "Unknow\t"
  93. });
  94. string[] message = logInfo.Message.Replace(Environment.NewLine, ";").Split(';');
  95. sb.Append(message[0]);
  96. this._logWriter?.WriteLine(sb);
  97. for (int i = 1; i < message.Length; i++)
  98. this._logWriter?.WriteLine($"\t\t\t\t\t\t\t{message[i]}");
  99. if (message.Length > 1)
  100. this._logWriter?.WriteLine();
  101. _logWriter?.Flush();
  102. #if DEBUG
  103. Debug.WriteLine(this._name + sb);
  104. Console.WriteLine(this._name + sb);
  105. for (int i = 1; i < message.Length; i++)
  106. {
  107. Debug.WriteLine($"\t\t\t\t\t\t\t{message[i]}");
  108. Console.WriteLine($"\t\t\t\t\t\t\t{message[i]}");
  109. }
  110. #endif
  111. //On Dispose
  112. if (this._quitEvent.WaitOne(0) && _logEventQueue?.Count == 0)
  113. {
  114. this._logEventQueue = null;
  115. this._logWriter?.Dispose();
  116. this._logStream?.Dispose();
  117. _exitWait.Set();
  118. }
  119. }
  120. public void Dispose()
  121. {
  122. this._quitEvent.Set();
  123. _exitWait.WaitOne();
  124. }
  125. }