소스 검색

add MetrologService

姜青儀 2 년 전
부모
커밋
df95d45ddb
3개의 변경된 파일140개의 추가작업 그리고 0개의 파일을 삭제
  1. 5 0
      CMSCollection/CMSCollection.cs
  2. 4 0
      KdanCommon.csproj
  3. 131 0
      Log/MetroLogService.cs

+ 5 - 0
CMSCollection/CMSCollection.cs

@@ -40,6 +40,11 @@ namespace KdanCommon.CMSCollection
             settingList = new List<ISetting>();
         }
 
+        ~CMSCollection()
+        {
+            httpClient.Dispose();
+        }
+
         #region Public Method
         public async Task<ISetting> GetSetting(string settingId)
         {

+ 4 - 0
KdanCommon.csproj

@@ -129,10 +129,14 @@
     <Compile Include="Helpers\DynamicJsonHelper.cs" />
     <Compile Include="Helpers\HttpClientHelper.cs" />
     <Compile Include="Helpers\JsonTool.cs" />
+    <Compile Include="Log\MetroLogService.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <EmbeddedResource Include="Properties\KdanCommon.rd.xml" />
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="MetroLog">
+      <Version>1.0.1</Version>
+    </PackageReference>
     <PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
       <Version>6.2.13</Version>
     </PackageReference>

+ 131 - 0
Log/MetroLogService.cs

@@ -0,0 +1,131 @@
+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}";
+        }
+    }
+}