DefaultLogEngine.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace Flyinsono.Client.Test
  5. {
  6. public class DefaultLogEngine : LogEngine
  7. {
  8. private const string InfoTag = "I ; ";
  9. private const string DebguTag = "D ; ";
  10. private const string WarnTag = "W ; ";
  11. private const string ErrorTag = "E ; ";
  12. private const string VerboseTag = "V ; ";
  13. private const string UserOperationTag = "U ; ";
  14. private StreamWriter _logWriter;
  15. private readonly object _mutex = new object();
  16. public static string DataFolder;
  17. private DateTime _date;
  18. private bool _disposed;
  19. public DefaultLogEngine()
  20. {
  21. }
  22. ~DefaultLogEngine()
  23. {
  24. //Dispose the StreamWriter when dispose.
  25. DoDispose();
  26. }
  27. private void DoDispose()
  28. {
  29. if (!_disposed)
  30. {
  31. _disposed = true;
  32. }
  33. }
  34. protected void InitializeLogger()
  35. {
  36. var logDirectory = Path.Combine(DataFolder, "Logs");
  37. if (!Directory.Exists(logDirectory))
  38. {
  39. Directory.CreateDirectory(logDirectory);
  40. }
  41. var now = DateTime.Now;
  42. var logTag = $"{now:yyyyMMddHHmmss}_{now.Millisecond:d3}";
  43. var logFileName = $"Log_{logTag}.log";
  44. var logFilePath = Path.Combine(logDirectory, logFileName);
  45. if (!File.Exists(logFilePath))
  46. {
  47. _logWriter?.Close();
  48. var logFileStream = new FileStream(logFilePath,FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
  49. _logWriter = new StreamWriter(logFileStream);
  50. }
  51. else
  52. {
  53. _logWriter?.Close();
  54. var logFileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  55. logFileStream.Position = logFileStream.Length;
  56. _logWriter = new StreamWriter(logFileStream);
  57. }
  58. }
  59. public override void Write(LogLevel level, string msg)
  60. {
  61. lock (_mutex)
  62. {
  63. if (DateTime.Today != _date)
  64. {
  65. InitializeLogger();
  66. _date = DateTime.Today;
  67. }
  68. try
  69. {
  70. var message = RecombinateMessage(level, msg);
  71. WriteMessage(message);
  72. }
  73. catch (Exception)
  74. {
  75. // There is exception during writing log, so just ignore
  76. }
  77. }
  78. }
  79. protected void WriteMessage(string message)
  80. {
  81. _logWriter.WriteLine(message);
  82. _logWriter.Flush();
  83. }
  84. protected string RecombinateMessage(LogLevel level, string msg)
  85. {
  86. var now = DateTime.Now;
  87. StringBuilder sb = new StringBuilder();
  88. sb.Append(
  89. $"[{now:yyyyMMddHHmmss},{now.Millisecond:d3}]-");
  90. string message = msg;
  91. if (level == LogLevel.Error)
  92. {
  93. sb.Append(ErrorTag);
  94. }
  95. else if (level == LogLevel.Warn)
  96. {
  97. sb.Append(WarnTag);
  98. }
  99. else if (level == LogLevel.Info)
  100. {
  101. sb.Append(InfoTag);
  102. }
  103. else if (level == LogLevel.Verbose)
  104. {
  105. sb.Append(VerboseTag);
  106. }
  107. else if (level == LogLevel.Debug)
  108. {
  109. sb.Append(DebguTag);
  110. }
  111. else if (level == LogLevel.UserOperation)
  112. {
  113. sb.Append(UserOperationTag);
  114. }
  115. sb.Append(message);
  116. return sb.ToString();
  117. }
  118. public override void Dispose()
  119. {
  120. DoDispose();
  121. GC.SuppressFinalize(this);
  122. }
  123. }
  124. }