lith 3 anni fa
parent
commit
e4700a7229

+ 12 - 1
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/appsettings.json

@@ -7,7 +7,18 @@
       /* print the log to Log/*.txt default:true  */
       "PrintToTxt": true,
       /* print the log to console. default:true  */
-      "PrintToConsole": true
+      "PrintToConsole": true,
+
+      /* [optional]send log to splunk */
+      "//Splunk": {
+        "url": "http://192.168.20.20:8088/services/collector",
+        "authToken": "xxxx",
+
+        "message": {
+          "index": "prod",
+          "//source": {}
+        }
+      }
     }
   },
 

+ 2 - 2
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/ConsoleCollector.cs

@@ -12,9 +12,9 @@ namespace Vit.Core.Module.Log.LogCollector
             #region build log string
             string logString = msg.message;
 
-            if (msg.objs != null)
+            if (msg.metadata != null)
             {
-                foreach (var obj in msg.objs)
+                foreach (var obj in msg.metadata)
                 {
                     logString += Environment.NewLine + obj.Serialize();
                 }

+ 90 - 0
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/SplunkCollector.cs

@@ -0,0 +1,90 @@
+using System;
+using System.Net.Http;
+using System.Net.Http.Headers;
+
+using Vit.Extensions;
+
+namespace Vit.Core.Module.Log.LogCollector
+{
+    public class SplunkCollector : ILogCollector
+    {
+        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
+        public void Write(LogMessage msg)
+        {
+            var msgBody = new
+            {
+                index = message?.index,
+                level = msg.level,
+                message = msg.message,
+                metadata = msg.metadata,
+                time = DateTime.Now,
+                source = message?.source
+            };
+
+            var request = new HttpRequestMessage(HttpMethod.Post, url);
+            request.Content = new StringContent(msgBody.Serialize(), Vit.Core.Module.Serialization.Serialization_Newtonsoft.defaultEncoding, "application/json");
+
+            // TODO:    retry when fail. 
+            //          batch:  batchIntervalInSeconds, batchSizeLimit, queueLimit
+            var response = httpClient.SendAsync(request);
+        }
+
+        public class Message 
+        {
+            /// <summary>
+            /// 
+            /// </summary>
+            public string index;
+
+            public object source;
+
+        }
+        public Message message;
+
+        /// <summary>
+        /// 接口地址,如 "http://192.168.20.20:8088/services/collector"
+        /// </summary>
+        public string url;
+
+        public string authToken;
+
+        /// <summary>
+        /// 默认 "Splunk"
+        /// </summary>
+        public string AUTH_SCHEME;
+        /// <summary>
+        /// 默认 "X-Splunk-Request-Channel"
+        /// </summary>
+        public string SPLUNK_REQUEST_CHANNEL;
+
+        #region HttpClient
+
+        HttpClient _httpClient = null;
+
+
+        public HttpClient httpClient 
+        {
+            get 
+            {
+                if (_httpClient == null) 
+                {
+                    _httpClient = new HttpClient();
+                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AUTH_SCHEME ?? "Splunk", authToken);
+                    if (!_httpClient.DefaultRequestHeaders.Contains(SPLUNK_REQUEST_CHANNEL ?? "X-Splunk-Request-Channel"))
+                    {
+                        _httpClient.DefaultRequestHeaders.Add(SPLUNK_REQUEST_CHANNEL ?? "X-Splunk-Request-Channel", Guid.NewGuid().ToString());
+                    }
+                }
+                return _httpClient;
+            }
+        }
+
+        #endregion
+
+
+
+
+
+
+    }
+}

+ 2 - 2
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/TxtCollector.cs

@@ -14,9 +14,9 @@ namespace Vit.Core.Module.Log.LogCollector
             #region build log string
             string logString = DateTime.Now.ToString("[HH:mm:ss.ffff]") + msg.message + NewLine;
 
-            if (msg.objs != null)
+            if (msg.metadata != null)
             {
-                foreach (var obj in msg.objs)
+                foreach (var obj in msg.metadata)
                 {
                     logString += obj.Serialize() + NewLine;
                 }

+ 1 - 1
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogMessage.cs

@@ -8,6 +8,6 @@ namespace Vit.Core.Module.Log
     {
         public Level level;
         public string message;
-        public IEnumerable<Object> objs;
+        public IEnumerable<Object> metadata;
     }
 }

+ 11 - 11
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogMng.Extensions.cs

@@ -13,22 +13,22 @@ namespace Vit.Core.Module.Log
         /// DEBUG (调试信息):记录系统用于调试的一切信息,内容或者是一些关键数据内容的输出
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public void Debug(string message, params object[] objs)
+        public void Debug(string message, params object[] metadata)
         {
-            Log(Level.DEBUG, message, objs);
+            Log(Level.DEBUG, message, metadata);
         }
 
         /// <summary>
         /// INFO(一般信息):记录系统运行中应该让用户知道的基本信息。例如,服务开始运行,功能已经开户等。
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public void Info(string message, params object[] objs)
+        public void Info(string message, params object[] metadata)
         {
-            Log(Level.INFO, message, objs);
+            Log(Level.INFO, message, metadata);
         }
 
 
@@ -36,18 +36,18 @@ namespace Vit.Core.Module.Log
         /// ERROR(一般错误):记录系统中出现的导致系统不稳定,部分功能出现混乱或部分功能失效一类的错误。例如,数据字段为空,数据操作不可完成,操作出现异常等。
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public void Error(string message, params object[] objs)
+        public void Error(string message, params object[] metadata)
         {
-            Log(Level.ERROR, message,objs);
+            Log(Level.ERROR, message,metadata);
         }
 
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public void Warn(string message, params object[] objs)
+        public void Warn(string message, params object[] metadata)
         {
-            Log(Level.WARN, message, objs);
+            Log(Level.WARN, message, metadata);
         }
 
 

+ 4 - 4
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogMng.cs

@@ -18,7 +18,7 @@ namespace Vit.Core.Module.Log
         }
         public void AddCollector(Action<LogMessage> OnLog)
         {
-            collectors.Add(new LogCollector.Collector { OnLog= OnLog });
+            collectors.Add(new LogCollector.Collector { OnLog = OnLog });
         }
 
 
@@ -34,15 +34,15 @@ namespace Vit.Core.Module.Log
             catch { }
         }
         #endregion
- 
+
 
 
         #region Log
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public void Log(Level level, string message, params object[] objs)
+        public void Log(Level level, string message, params object[] metadata)
         {
-            Log(new LogMessage { level = level, message = message, objs = objs });
+            Log(new LogMessage { level = level, message = message, metadata = metadata });
         }
         #endregion
 

+ 20 - 13
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/Logger.cs

@@ -27,6 +27,13 @@ namespace Vit.Core.Module.Log
             {
                 PrintToConsole = true;
             }
+
+            //splunk
+            var splunk = ConfigurationManager.Instance.GetByPath<SplunkCollector>("Vit.Logger.Splunk");
+            if (splunk!=null)
+            {
+                log.collectors.Add(splunk);
+            }
         }
         #endregion
 
@@ -84,9 +91,9 @@ namespace Vit.Core.Module.Log
         }
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static void Log(Level level, string message, params object[] objs)
+        public static void Log(Level level, string message, params object[] metadata)
         {
-            log.Log(level, message, objs);
+            log.Log(level, message, metadata);
         }
         #endregion
 
@@ -97,18 +104,18 @@ namespace Vit.Core.Module.Log
         /// DEBUG (调试信息):记录系统用于调试的一切信息,内容或者是一些关键数据内容的输出
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static void Debug(string message, params object[] objs)
+        public static void Debug(string message, params object[] metadata)
         {
-            log.Debug(message, objs);
+            log.Debug(message, metadata);
         }
 
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static void Warn(string message, params object[] objs)
+        public static void Warn(string message, params object[] metadata)
         {
-            log.Warn(message, objs);
+            log.Warn(message, metadata);
         }
 
 
@@ -118,11 +125,11 @@ namespace Vit.Core.Module.Log
         /// INFO(一般信息):记录系统运行中应该让用户知道的基本信息。例如,服务开始运行,功能已经开户等。
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static void Info(string message, params object[] objs)
+        public static void Info(string message, params object[] metadata)
         {
-            log.Info(message, objs);
+            log.Info(message, metadata);
         }
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
@@ -140,11 +147,11 @@ namespace Vit.Core.Module.Log
         /// ERROR(一般错误):记录系统中出现的导致系统不稳定,部分功能出现混乱或部分功能失效一类的错误。例如,数据字段为空,数据操作不可完成,操作出现异常等。
         /// </summary>
         /// <param name="message"></param>
-        /// <param name="objs"></param>
+        /// <param name="metadata"></param>
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        public static void Error(string message, params object[] objs)
+        public static void Error(string message, params object[] metadata)
         {
-            log.Error(message, objs);
+            log.Error(message, metadata);
         }
 
         /// <summary>