WriterRepository.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace XGame.Framework.Logger
  5. {
  6. internal class WriterRepository : IDisposable
  7. {
  8. private Dictionary<LogType, ILogWriter> _writerMap = new Dictionary<LogType, ILogWriter>();
  9. public ILogWriter GetWriter(LogType logType)
  10. {
  11. ILogWriter writer = null;
  12. lock (_writerMap)
  13. {
  14. if (!_writerMap.TryGetValue(logType, out writer))
  15. {
  16. var logDirectory = logType == LogType.Code ? LogDefine.CodeLogPath : LogDefine.EventLogPath;
  17. if (!Directory.Exists(logDirectory))
  18. {
  19. Directory.CreateDirectory(logDirectory);
  20. }
  21. var mmfile = new MMFile(logType == LogType.Code ? LogDefine.CodeMMFPath : LogDefine.EventMMFPath, LogDefine.CacheCapacityLimit);
  22. writer = new LogWriter(logType, mmfile, null);
  23. _writerMap.Add(logType, writer);
  24. }
  25. }
  26. return writer;
  27. }
  28. public void Dispose()
  29. {
  30. lock (_writerMap)
  31. {
  32. foreach (var item in _writerMap)
  33. {
  34. (item.Value as IDisposable)?.Dispose();
  35. }
  36. _writerMap.Clear();
  37. }
  38. }
  39. }
  40. }