MetroLogService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using MetroLog;
  2. using MetroLog.Layouts;
  3. using MetroLog.Targets;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Windows.Storage;
  10. using Windows.System;
  11. namespace KdanCommon.Log
  12. {
  13. public sealed class MetroLogService
  14. {
  15. private static MetroLogService _instance = new MetroLogService();
  16. public static MetroLogService Instance
  17. {
  18. get
  19. {
  20. return _instance;
  21. }
  22. }
  23. public LogLevel MinLevel = LogLevel.Trace;
  24. public LogLevel MaxLevel = LogLevel.Fatal;
  25. private ILogManager currentLogManager = null;
  26. public bool IsWriteLog = true;
  27. private MetroLogService()
  28. {
  29. }
  30. public void Init(string appName, bool isWriteLog = true)
  31. {
  32. IsWriteLog = isWriteLog;
  33. var customLogLayout = new CustomLogLayout(appName);
  34. var loggingConfiguration = new LoggingConfiguration { IsEnabled = true };
  35. loggingConfiguration.AddTarget(MinLevel, MaxLevel, new StreamingFileTarget(customLogLayout));
  36. currentLogManager = LogManagerFactory.CreateLogManager(loggingConfiguration);
  37. }
  38. private ILogger GetCurrentLogger<T>()
  39. {
  40. return currentLogManager.GetLogger<T>();
  41. }
  42. public void WriteTraceLog<T>(string msg, Exception ex = null)
  43. {
  44. System.Diagnostics.Debug.Write(msg + " " + ex);
  45. if (!IsWriteLog)
  46. return;
  47. var log = GetCurrentLogger<T>();
  48. log.Trace(msg, ex);
  49. }
  50. public void WriteDebugLog<T>(string msg, Exception ex = null)
  51. {
  52. System.Diagnostics.Debug.Write(msg + " " + ex);
  53. if (!IsWriteLog)
  54. return;
  55. var log = GetCurrentLogger<T>();
  56. log.Debug(msg, ex);
  57. }
  58. public void WriteInfoLog<T>(string msg, Exception ex = null)
  59. {
  60. System.Diagnostics.Debug.Write(msg + " " + ex);
  61. if (!IsWriteLog)
  62. return;
  63. var log = GetCurrentLogger<T>();
  64. log.Info(msg, ex);
  65. }
  66. public void WriteWarnLog<T>(string msg, Exception ex = null)
  67. {
  68. System.Diagnostics.Debug.Write(msg + " " + ex);
  69. if (!IsWriteLog)
  70. return;
  71. var log = GetCurrentLogger<T>();
  72. log.Warn(msg, ex);
  73. }
  74. public void WriteErrorLog<T>(string msg, Exception ex = null)
  75. {
  76. System.Diagnostics.Debug.Write(msg + " " + ex);
  77. if (!IsWriteLog)
  78. return;
  79. var log = GetCurrentLogger<T>();
  80. log.Error(msg, ex);
  81. }
  82. public void WriteFatalLog<T>(string msg, Exception ex = null)
  83. {
  84. System.Diagnostics.Debug.Write(msg + " " + ex);
  85. if (!IsWriteLog)
  86. return;
  87. var log = GetCurrentLogger<T>();
  88. log.Fatal(msg, ex);
  89. }
  90. public async Task OpenLogFolder()
  91. {
  92. var tmpFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("MetroLogs", CreationCollisionOption.OpenIfExists);
  93. if (tmpFolder != null)
  94. await Launcher.LaunchFolderAsync(tmpFolder);
  95. }
  96. }
  97. public class CustomLogLayout : Layout
  98. {
  99. private string _appName;
  100. public CustomLogLayout(string appName)
  101. {
  102. _appName = appName;
  103. }
  104. public override string GetFormattedString(LogWriteContext context, LogEventInfo info)
  105. {
  106. return $"{_appName}|{info.TimeStamp.LocalDateTime}|{info.Level}|{info.Logger}|{info.Message}|{info.Exception}";
  107. }
  108. }
  109. }