123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using MetroLog;
- using MetroLog.Layouts;
- using MetroLog.Targets;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.Storage;
- using Windows.System;
- namespace KdanCommon.Log
- {
- public sealed class MetroLogService
- {
- private static MetroLogService _instance = new MetroLogService();
- public static MetroLogService Instance
- {
- get
- {
- return _instance;
- }
- }
- public LogLevel MinLevel = LogLevel.Trace;
- public LogLevel MaxLevel = LogLevel.Fatal;
- private ILogManager currentLogManager = null;
- public bool IsWriteLog = true;
- private MetroLogService()
- {
- }
- public void Init(string appName, bool isWriteLog = true)
- {
- IsWriteLog = isWriteLog;
- var customLogLayout = new CustomLogLayout(appName);
- var loggingConfiguration = new LoggingConfiguration { IsEnabled = true };
- loggingConfiguration.AddTarget(MinLevel, MaxLevel, new StreamingFileTarget(customLogLayout));
- currentLogManager = LogManagerFactory.CreateLogManager(loggingConfiguration);
- }
- private ILogger GetCurrentLogger<T>()
- {
- return currentLogManager.GetLogger<T>();
- }
- public void WriteTraceLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Trace(msg, ex);
- }
- public void WriteDebugLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Debug(msg, ex);
- }
- public void WriteInfoLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Info(msg, ex);
- }
- public void WriteWarnLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Warn(msg, ex);
- }
- public void WriteErrorLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Error(msg, ex);
- }
- public void WriteFatalLog<T>(string msg, Exception ex = null)
- {
- System.Diagnostics.Debug.Write(msg + " " + ex);
- if (!IsWriteLog)
- return;
- var log = GetCurrentLogger<T>();
- log.Fatal(msg, ex);
- }
- public async Task OpenLogFolder()
- {
- var tmpFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("MetroLogs", CreationCollisionOption.OpenIfExists);
- if (tmpFolder != null)
- await Launcher.LaunchFolderAsync(tmpFolder);
- }
- }
- public class CustomLogLayout : Layout
- {
- private string _appName;
- public CustomLogLayout(string appName)
- {
- _appName = appName;
- }
- public override string GetFormattedString(LogWriteContext context, LogEventInfo info)
- {
- return $"{_appName}|{info.TimeStamp.LocalDateTime}|{info.Level}|{info.Logger}|{info.Message}|{info.Exception}";
- }
- }
- }
|