Jelajahi Sumber

Add logging to Common

Dalibor Votruba 5 tahun lalu
induk
melakukan
097ea717d4

+ 7 - 0
Common/Common.csproj

@@ -55,6 +55,13 @@
     <Compile Include="Console\ConsoleWriter.cs" />
     <Compile Include="Data\CsvHelper.cs" />
     <Compile Include="Data\Extensions\SqlCommandExt.cs" />
+    <Compile Include="Log\Extensions\ObjectExt.cs" />
+    <Compile Include="Log\ILogging.cs" />
+    <Compile Include="Log\ILoggingProvider.cs" />
+    <Compile Include="Log\Logging.cs" />
+    <Compile Include="Log\Providers\AbstractLoggingProvider.cs" />
+    <Compile Include="Log\Providers\DummyLoggingProvider.cs" />
+    <Compile Include="Log\SeverityEnum.cs" />
     <Compile Include="Object\Singleton.cs" />
     <Compile Include="Object\WeakReference.cs" />
     <Compile Include="Reflection\TypePropertyLocator.cs" />

+ 81 - 0
Common/Log/Extensions/ObjectExt.cs

@@ -0,0 +1,81 @@
+using System;
+using System.CodeDom;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+namespace Quadarax.Foundation.Common.Log.Extensions
+{
+    public static class ObjectExt
+    {
+        private static IDictionary<string, ILoggingProvider> _providerCache = new ConcurrentDictionary<string, ILoggingProvider>();
+
+        public static void Log(this object owner, SeverityEnum severity, int code, string message)
+        {
+            if (owner == null)
+                throw new ArgumentNullException(nameof(owner));
+            var provider = GetTypeRegistration(owner);
+
+
+        }
+
+        public static void Log(this object owner, SeverityEnum severity, string message)
+        {
+
+        }
+
+        public static void LogInfo(string message)
+        {
+
+        }
+
+        public static void LogDebug(int code, string message)
+        {
+
+        }
+
+        public static void LogDebug(string message)
+        {
+
+        }
+
+        public static void LogError(int code, string message, Exception e)
+        {
+
+        }
+
+        public static void LogError(string message, Exception e)
+        {
+
+        }
+        public static void LogError(Exception e)
+        {
+
+        }
+
+        public static void RegisterThisProvider(this object owner, ILoggingProvider provider = null)
+        {
+            var typeName = owner.GetType().FullName;
+            if (provider == null)
+                provider = ILoggingProvider.CreateDefault();
+            _providerCache.Add(typeName, provider);
+        }
+
+
+        private static void RegisterType(Type type, ILoggingProvider provider)
+        {
+
+        }
+
+
+        private static ILoggingProvider GetTypeRegistration(object owner, bool createIfNotExists = true)
+        {
+            var typeName = owner.GetType().FullName;    
+            if (!_providerCache.TryGetValue(typeName, out var provider))
+            {
+
+            }
+        }
+
+
+    }
+}

+ 12 - 0
Common/Log/ILogging.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Quadarax.Foundation.Common.Log
+{
+    public interface ILogging
+    {
+    }
+}

+ 11 - 0
Common/Log/ILoggingProvider.cs

@@ -0,0 +1,11 @@
+using System;
+
+namespace Quadarax.Foundation.Common.Log
+{
+    public interface ILoggingProvider
+    {
+        bool Enabled { get; set; }
+
+        void Log(SeverityEnum severity, int? code, string message, Exception e = null);
+    }
+}

+ 12 - 0
Common/Log/Logging.cs

@@ -0,0 +1,12 @@
+using Quadarax.Foundation.Common.Log.Providers;
+using Quadarax.Foundation.Common.Object.Bee.Core.Pattern;
+
+namespace Quadarax.Foundation.Common.Log
+{
+    public class Logging : Singleton<>
+    {
+        public static ILoggingProvider Provider { get; set; }
+
+       
+    }
+}

+ 45 - 0
Common/Log/Providers/AbstractLoggingProvider.cs

@@ -0,0 +1,45 @@
+using System;
+
+namespace Quadarax.Foundation.Common.Log.Providers
+{
+    public abstract class AbstractLoggingProvider : ILoggingProvider
+    {
+        public bool Enabled { get; set; }
+        
+        public abstract void Log(SeverityEnum severity, int? code, string message, Exception e = null);
+
+        protected virtual string GetMessageString(SeverityEnum severity, int? code, string message, Exception e = null)
+        {
+            if (e == null)
+                if (code == null)
+                    return TranslateMessage(message);
+                else
+                    return $"[{TranslateCode(code)}] {TranslateMessage(message)}";
+            else
+                if (code == null)
+                    return TranslateMessage(message);
+                else
+                    return $"[{TranslateCode(code)}] {TranslateMessage(message)} : {TranslateException(e)}";
+        }
+
+        protected virtual string TranslateCode(int? code)
+        {
+            return code.HasValue ? code.ToString() : string.Empty;
+        }
+
+        protected virtual string TranslateMessage(string message)
+        {
+            return message;
+        }
+
+        protected virtual string TranslateSeverity(SeverityEnum severity)
+        {
+            return severity.ToString();
+        }
+
+        protected virtual string TranslateException(Exception e)
+        {
+            return e == null ? string.Empty : e.ToString();
+        }
+    }
+}

+ 15 - 0
Common/Log/Providers/DummyLoggingProvider.cs

@@ -0,0 +1,15 @@
+
+using System;
+using System.Diagnostics;
+
+namespace Quadarax.Foundation.Common.Log.Providers
+{
+    public class DummyLoggingProvider : AbstractLoggingProvider
+    {
+        public bool Enabled { get; set; }
+        public override void Log(SeverityEnum severity, int? code, string message, Exception e = null)
+        {
+            Trace.WriteLine(GetMessageString(severity, code, message, e), severity.ToString());
+        }
+    }
+}

+ 13 - 0
Common/Log/SeverityEnum.cs

@@ -0,0 +1,13 @@
+namespace Quadarax.Foundation.Common.Log
+{
+    public enum SeverityEnum
+    {
+        Trace,
+        Debug,
+        Info,
+        Audit,
+        Warning,
+        Error,
+        Critical
+    }
+}