lith 3 years ago
parent
commit
d5e63f1b5c

+ 1 - 1
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/LoggerTest.cs

@@ -21,7 +21,7 @@ namespace Vit.Core.MsTest.Module
             //»á½«ÈÕ־дÈë /Logs/{yyyy-MM}/{yyyy-MM-dd}Error.log
             Logger.Error("hello world!");
             Logger.Error(new Exception("hello world!"));
-            Logger.Error("error",new Exception("hello world!"));
+            Logger.Error("error", new Exception("hello world!"), new { a = 10 }, "message2", 12);
             Logger.Error(new SsError { errorCode = 404, errorMessage = "hello world!", errorTag = "150721_lith_1" });
             Logger.Error("error",new SsError { errorCode = 404, errorMessage = "hello world!", errorTag = "150721_lith_1" });
 

+ 5 - 7
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/appsettings.json

@@ -17,15 +17,10 @@
       "Collector": [
         {
           /* 在此Assembly中加载类 */
-          "assemblyFile": "Vit.Core.dll"
+          "assemblyFile": "Vit.Core.dll",
           /* 动态加载的类名,必须继承接口 Vit.Core.Module.Log.LogCollector.ILogCollector */
           //"className": "Vit.Core.Module.Log.LogCollector.Splunk.SplunkCollector",
 
-          //custome config
-        },
-        {
-          //"className": "SplunkCollector",
-
           "client": {
             "url": "https://192.168.20.20:8088/services/collector",
             "authToken": "xxxxx",
@@ -49,7 +44,10 @@
           }
         },
         {
-          //"className": "ElasticSearchCollector",
+          /* 在此Assembly中加载类 */
+          "assemblyFile": "Vit.Core.dll",
+          /* 动态加载的类名,必须继承接口 Vit.Core.Module.Log.LogCollector.ILogCollector */
+          "className": "Vit.Core.Module.Log.LogCollector.ElasticSearch.ElasticSearchCollector",
 
           "client": {
             // es address, example:"http://192.168.20.20:9200"

+ 12 - 2
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/ElasticSearch/ElasticSearchCollector.cs

@@ -3,6 +3,7 @@
 using System;
 
 using Vit.Extensions;
+using System.Linq;
 
 namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
 {
@@ -27,8 +28,6 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Write(Log.LogMessage msg)
         {
-            if (msg.metadata != null && msg.metadata.Length == 0) msg.metadata = null;
-
             var record = new LogMessage
             {
                 Time = DateTime.UtcNow,
@@ -37,6 +36,17 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
                 metadata = msg.metadata,
                 appInfo = appInfo
             };
+
+            if (record.metadata != null)
+            {
+                if (record.metadata.Length == 0)
+                    record.metadata = null;
+                else
+                {
+                    record.metadata = record.metadata.Select(m => m?.IsValueTypeOrStringType() == true ? new { value = m } : m).ToArray();
+                }
+            }
+
             client.SendAsync(record);
         }
 

+ 12 - 11
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/ElasticSearch/LogClient.cs

@@ -109,24 +109,25 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
         private StringBuilder buffer = new StringBuilder();
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
-        private void SendToServer<T>(IEnumerable<T> records)
+        private void SendToServer(IEnumerable<LogMessage> records)
         {
-            buffer.Clear();
-            foreach (var record in records)
+            var request = new HttpRequestMessage(HttpMethod.Post, bulkUrl);
+            lock (buffer)
             {
-                buffer.AppendLine("{\"create\":{}}").AppendLine(record.Serialize());
+                buffer.Clear();
+                foreach (var record in records)
+                {
+                    buffer.AppendLine("{\"create\":{}}").AppendLine(record.Serialize());
+                }
+                request.Content = new StringContent(buffer.ToString(), Vit.Core.Module.Serialization.Serialization_Newtonsoft.defaultEncoding, "application/json");
+                buffer.Clear();
             }
-
-            var request = new HttpRequestMessage(HttpMethod.Post, bulkUrl);
-            request.Content = new StringContent(buffer.ToString(), Vit.Core.Module.Serialization.Serialization_Newtonsoft.defaultEncoding, "application/json");
-            buffer.Clear();
-
             // TODO:    retry when fail. 
             //          batch:  batchIntervalInSeconds, batchSizeLimit, queueLimit
-            //httpClient.SendAsync(request);
+            httpClient.SendAsync(request);
 
 
-            var reply = httpClient.SendAsync(request).Result;
+            //var reply = httpClient.SendAsync(request).Result;
         }
     }
 }

+ 18 - 14
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/ElasticSearch/LogMessage.cs

@@ -2,8 +2,6 @@
 
 using System;
 
-using Vit.Extensions;
-
 namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
 {
     internal class LogMessage
@@ -11,7 +9,8 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
         /* ElasticSearchMessage Format:
         // "index": "dev", "type": "_doc",
           {
-              "time": 1426279439.123, 
+              "@timestamp":1653468236619,
+              "time": "2022-05-25T08:19:36.686Z", 
               "level": "info",
               "message": "Something happened",
               "metadata": [],
@@ -24,9 +23,12 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
               }
           }
       */
+        [JsonProperty("@timestamp", NullValueHandling = NullValueHandling.Ignore)]
+        public string timestamp;
+
 
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-        public double? time;
+        public string time;
 
 
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
@@ -36,23 +38,25 @@ namespace Vit.Core.Module.Log.LogCollector.ElasticSearch
         public string message;
 
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-        public object metadata;
+        public Object[] metadata;
 
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
         public object appInfo;
 
 
-        public DateTime Time { set => time = ToEpoch(value); }
-        
+        public DateTime Time 
+        {
+            set
+            {
+                //timestamp = value.ToTimeStamp();
+                timestamp = time = ToTimeString(value);
+            }
+        }
+
 
-        public static double ToEpoch(DateTime value)
+        public static string ToTimeString(DateTime value)
         {
-            // From Splunk HTTP Collector Protocol
-            // The default time format is epoch time format, in the format <sec>.<ms>. 
-            // For example, 1433188255.500 indicates 1433188255 seconds and 500 milliseconds after epoch, 
-            // or Monday, June 1, 2015, at 7:50:55 PM GMT.
-            // See: http://dev.splunk.com/view/SP-CAAAE6P
-            return value.ToTimeStamp() / 1000.0;
+            return value.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
         }
     }
 }

+ 45 - 24
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/Splunk/LogMessage.cs

@@ -7,27 +7,31 @@ using Vit.Extensions;
 namespace Vit.Core.Module.Log.LogCollector.Splunk
 {
     internal class LogMessage
-    {/*
-             {
-                "time": 1426279439.123,  
-                "host": "localhost",
-                "source": "random-data-generator",
-                "sourcetype": "my_sample_data",
-                "index": "dev",
-                "event": { 
-                    "level": "info",
-                    "message": "Something happened",
-                    "metadata": [],
-                     //custome object
-                    "app": {
-                      "namespace": "mc.sers.cloud",
-                      "appName": "mc",
-                      "moduleName": "sers"
-                      //,"...": {}
-                    }
-                }
-             }
-             */
+    {
+
+        /* SplunkMessage Format:
+        {
+           "time": 1426279439.123,  
+           "host": "localhost",
+           "source": "random-data-generator",
+           "sourcetype": "my_sample_data",
+           "index": "dev",
+           "event": { 
+               "level": "info",
+               "message": "Something happened",
+               "metadata": [],
+                //custome object
+               "appInfo": {
+                 "namespace": "mc.sers.cloud",
+                 "appName": "mc",
+                 "moduleName": "sers"
+                 //,"...": {}
+               }
+           }
+        }
+        */
+
+
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
         public double? time;
 
@@ -44,13 +48,12 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
         public string sourcetype;
 
         [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-        public object @event;
-
+        public Event @event;
 
 
 
         public DateTime Time { set => time = ToEpoch(value); }
-        
+
 
         public static double ToEpoch(DateTime value)
         {
@@ -62,4 +65,22 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
             return value.ToTimeStamp() / 1000.0;
         }
     }
+
+
+    internal class Event
+    {
+
+        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+        public string level;
+
+        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+        public string message;
+
+        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+        public Object[] metadata;
+
+        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+        public object appInfo;
+    }
+
 }

+ 2 - 41
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/Splunk/SplunkCollector.cs

@@ -25,30 +25,6 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
 
 
 
-
-        /* SplunkMessage Format:
-            {
-               "time": 1426279439.123,  
-               "host": "localhost",
-               "source": "random-data-generator",
-               "sourcetype": "my_sample_data",
-               "index": "dev",
-               "event": { 
-                   "level": "info",
-                   "message": "Something happened",
-                   "metadata": [],
-                    //custome object
-                   "appInfo": {
-                     "namespace": "mc.sers.cloud",
-                     "appName": "mc",
-                     "moduleName": "sers"
-                     //,"...": {}
-                   }
-               }
-            }
-            */
-
-
         internal LogClient client;
         internal LogMessage message;
         public object appInfo;
@@ -57,8 +33,6 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Write(Log.LogMessage msg)
         {
-            if (msg.metadata != null && msg.metadata.Length == 0) msg.metadata = null;
-
             var record = new LogMessage
             {
                 Time = DateTime.UtcNow,
@@ -76,25 +50,12 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
                 }
             };
 
+            if (record.@event.metadata != null && record.@event.metadata.Length == 0) record.@event.metadata = null;
+
             client.SendAsync(record);
         }
 
 
-        internal class Event
-        {
-
-            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-            public string level;
-
-            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-            public string message;
-
-            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-            public object metadata;
-
-            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
-            public object appInfo;
-        }
 
 
 

+ 5 - 7
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/Vit.Logger.appsettings.json

@@ -14,15 +14,10 @@
       "Collector": [
         {
           /* 在此Assembly中加载类 */
-          "assemblyFile": "Vit.Core.dll"
+          "assemblyFile": "Vit.Core.dll",
           /* 动态加载的类名,必须继承接口 Vit.Core.Module.Log.LogCollector.ILogCollector */
           //"className": "Vit.Core.Module.Log.LogCollector.Splunk.SplunkCollector",
 
-          //custome config
-        },
-        {
-          //"className": "SplunkCollector",
-
           "client": {
             "url": "https://192.168.20.20:8088/services/collector",
             "authToken": "xxxxx",
@@ -46,7 +41,10 @@
           }
         },
         {
-          //"className": "ElasticSearchCollector",
+          /* 在此Assembly中加载类 */
+          "assemblyFile": "Vit.Core.dll",
+          /* 动态加载的类名,必须继承接口 Vit.Core.Module.Log.LogCollector.ILogCollector */
+          //"className": "Vit.Core.Module.Log.LogCollector.ElasticSearch.ElasticSearchCollector",
 
           "client": {
             // es address, example:"http://192.168.20.20:9200"