FileLogger.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading;
  7. namespace CZFW.Logger
  8. {
  9. public class FileLogger : ILogger
  10. {
  11. static protected string delimiter = new string(new char[] { (char)1 });
  12. public FileLogger(string categoryName)
  13. {
  14. this.Name = categoryName;
  15. }
  16. class Disposable : IDisposable
  17. {
  18. public void Dispose()
  19. {
  20. }
  21. }
  22. Disposable _DisposableInstance = new Disposable();
  23. public IDisposable BeginScope<TState>(TState state)
  24. {
  25. return _DisposableInstance;
  26. }
  27. public bool IsEnabled(LogLevel logLevel)
  28. {
  29. return this.MinLevel <= logLevel;
  30. }
  31. public void Reload()
  32. {
  33. _Expires = true;
  34. }
  35. public string Name { get; private set; }
  36. public LogLevel MinLevel { get; set; }
  37. public string FileDiretoryPath { get; set; }
  38. public string FileNameTemplate { get; set; }
  39. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
  40. {
  41. if (!this.IsEnabled(logLevel))
  42. return;
  43. var msg = formatter(state, exception);
  44. this.Write(logLevel, eventId, msg, exception);
  45. }
  46. void Write(LogLevel logLevel, EventId eventId, string message, Exception ex)
  47. {
  48. EnsureInitFile();
  49. //TODO 提高效率 队列写!!!
  50. var log = String.Concat(DateTime.Now.ToString("HH:mm:ss"), '[', logLevel.ToString(), ']', '[',
  51. Thread.CurrentThread.ManagedThreadId.ToString(), ',', eventId.Id.ToString(), ',', eventId.Name, ']',
  52. delimiter, message, delimiter, ex?.ToString());
  53. lock (this)
  54. {
  55. try
  56. {
  57. this._sw.WriteLine(log);
  58. }
  59. catch (Exception err)
  60. {
  61. _Expires = true;
  62. Console.WriteLine($"Log文件流写入异常,错误信息:{err.Message}");
  63. }
  64. }
  65. }
  66. bool _Expires = true;
  67. string _FileName;
  68. protected StreamWriter _sw;
  69. void EnsureInitFile()
  70. {
  71. if (CheckNeedCreateNewFile())
  72. {
  73. lock (this)
  74. {
  75. if (CheckNeedCreateNewFile())
  76. {
  77. InitFile();
  78. _Expires = false;
  79. }
  80. }
  81. }
  82. }
  83. bool CheckNeedCreateNewFile()
  84. {
  85. if (_Expires)
  86. {
  87. return true;
  88. }
  89. //TODO 使用 RollingType判断是否需要创建文件。提高效率!!!
  90. if (_FileName != DateTime.Now.ToString(this.FileNameTemplate))
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. void InitFile()
  97. {
  98. if (!Directory.Exists(this.FileDiretoryPath))
  99. {
  100. Directory.CreateDirectory(this.FileDiretoryPath);
  101. }
  102. var path = "";
  103. int i = 0;
  104. do
  105. {
  106. _FileName = DateTime.Now.ToString(this.FileNameTemplate);
  107. path = Path.Combine(this.FileDiretoryPath, _FileName + "_" + i + ".log");
  108. i++;
  109. } while (System.IO.File.Exists(path));
  110. var oldsw = _sw;
  111. _sw = new StreamWriter(new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read), Encoding.UTF8);
  112. _sw.AutoFlush = true;
  113. if (oldsw != null)
  114. {
  115. try
  116. {
  117. _sw.Flush();
  118. _sw.Dispose();
  119. }
  120. catch
  121. {
  122. }
  123. }
  124. }
  125. }
  126. }