FileLoggerProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace CZFW.Logger
  7. {
  8. public class FileLoggerProvider : ILoggerProvider, IDisposable
  9. {
  10. FileLoggerSettings _configuration;
  11. readonly ConcurrentDictionary<string, InitLoggerModel> _loggerKeys = new ConcurrentDictionary<string, InitLoggerModel>();
  12. readonly ConcurrentDictionary<string, FileLogger> _loggers = new ConcurrentDictionary<string, FileLogger>();
  13. public FileLoggerProvider(FileLoggerSettings configuration)
  14. {
  15. _configuration = configuration;
  16. _configuration.ChangeToken.RegisterChangeCallback(p =>
  17. {
  18. //appsettings.json changed. reload settings.
  19. _configuration.Reload();
  20. //update loggers settings form new settings
  21. foreach (var item in this._loggers.Values)
  22. {
  23. InitLoggerModel model = new InitLoggerModel();
  24. InitLoggerSettings(item.Name, model);
  25. InitLogger(model, item);
  26. }
  27. }, null);
  28. }
  29. public ILogger CreateLogger(string categoryName)
  30. {
  31. var loggerKey = this._loggerKeys.GetOrAdd(categoryName, p =>
  32. {
  33. InitLoggerModel model = new InitLoggerModel();
  34. InitLoggerSettings(categoryName, model);
  35. return model;
  36. });
  37. var key = loggerKey.FileDiretoryPath + loggerKey.FileNameTemplate;
  38. return this._loggers.GetOrAdd(key, p =>
  39. {
  40. var logger = new FileLogger(categoryName);
  41. InitLogger(loggerKey, logger);
  42. return logger;
  43. });
  44. }
  45. private static void InitLogger(InitLoggerModel model, FileLogger logger)
  46. {
  47. logger.FileNameTemplate = model.FileNameTemplate;
  48. logger.FileDiretoryPath = model.FileDiretoryPath;
  49. logger.MinLevel = model.MinLevel;
  50. }
  51. class InitLoggerModel
  52. {
  53. public LogLevel MinLevel { get; set; }
  54. public string FileDiretoryPath { get; set; }
  55. public string FileNameTemplate { get; set; }
  56. public override int GetHashCode()
  57. {
  58. return this.MinLevel.GetHashCode() + this.FileDiretoryPath.GetHashCode() + this.FileNameTemplate.GetHashCode();
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. var b = obj as InitLoggerModel;
  63. if (b == null)
  64. return false;
  65. return this.MinLevel == b.MinLevel && this.FileDiretoryPath == b.FileDiretoryPath && this.FileNameTemplate == b.FileNameTemplate;
  66. }
  67. }
  68. private void InitLoggerSettings(string categoryName, InitLoggerModel model)
  69. {
  70. model.MinLevel = LogLevel.Debug;
  71. var keys = this.GetKeys(categoryName);
  72. foreach (var item in keys)
  73. {
  74. var switchV = _configuration.GetSwitch(item);
  75. if (switchV.Item1)
  76. {
  77. model.MinLevel = switchV.Item2;
  78. break;
  79. }
  80. }
  81. model.FileDiretoryPath = this._configuration.DefaultPath;
  82. foreach (var item in keys)
  83. {
  84. var switchV = _configuration.GetDiretoryPath(item);
  85. if (switchV.Item1)
  86. {
  87. model.FileDiretoryPath = switchV.Item2;
  88. break;
  89. }
  90. }
  91. model.FileNameTemplate = this._configuration.DefaultFileName;
  92. foreach (var item in keys)
  93. {
  94. var switchV = _configuration.GetFileName(item);
  95. if (switchV.Item1)
  96. {
  97. model.FileNameTemplate = switchV.Item2;
  98. break;
  99. }
  100. }
  101. }
  102. IEnumerable<string> GetKeys(string categoryName)
  103. {
  104. while (!String.IsNullOrEmpty(categoryName))
  105. {
  106. // a.b.c
  107. //--result
  108. // a.b.c,a.b,a,Default
  109. yield return categoryName;
  110. var last = categoryName.LastIndexOf('.');
  111. if (last <= 0)
  112. {
  113. yield return "Default";
  114. yield break;
  115. }
  116. System.Diagnostics.Debug.WriteLine(categoryName + "--" + last);
  117. categoryName = categoryName.Substring(0, last);
  118. }
  119. yield break;
  120. }
  121. public void Dispose()
  122. {
  123. }
  124. }
  125. }