lith 3 лет назад
Родитель
Сommit
4060db9f08

+ 10 - 3
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/appsettings.json

@@ -10,13 +10,20 @@
       "PrintToConsole": true,
 
       /* [optional]send log to splunk */
-      "//Splunk": {
+      "Splunk": {
         "url": "http://192.168.20.20:8088/services/collector",
-        "authToken": "xxxx",
+        "authToken": "xxxxx",
 
         "message": {
           "index": "prod",
-          "//source": {}
+          "//host": "192.168.20.20:8088",
+          "//source": "http:mc",
+          "//sourcetype": "httpevent",
+
+          "//event": {
+            "namespace": "mc.sers.cloud",
+            "app": "mc"
+          }
         }
       }
     }

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

@@ -29,6 +29,6 @@ DEBUG (调试信息):记录系统用于调试的一切信息,内容或
     /// </summary>
     public enum Level
     {
-        OFF = 0, FATAL = 1, ERROR = 2, WARN = 3, INFO = 4, DEBUG = 5, ALL = 6,ApiTrace=7
+        off = 0, fatal = 1, error = 2, warn = 3, info = 4, debug = 5, all = 6, ApiTrace = 7
     }
 }

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

@@ -1,4 +1,6 @@
-using System;
+using Newtonsoft.Json;
+
+using System;
 using System.Net.Http;
 using System.Net.Http.Headers;
 
@@ -8,17 +10,43 @@ namespace Vit.Core.Module.Log.LogCollector
 {
     public class SplunkCollector : ILogCollector
     {
+        private static DateTimeOffset Epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
+        public static double ToEpoch(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;
+        }
+
+
+
+
+
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Write(LogMessage msg)
         {
-            var msgBody = new
+            if (msg.metadata != null && msg.metadata.Length == 0) msg.metadata = null;
+
+            var msgBody = new Message
             {
+                time = ToEpoch(DateTime.UtcNow),
                 index = message?.index,
-                level = msg.level,
-                message = msg.message,
-                metadata = msg.metadata,
-                time = DateTime.Now,
-                source = message?.source
+                host = message?.host ?? Environment.MachineName,
+                source = message?.source,
+                sourcetype = message?.sourcetype,
+
+                @event = new Message.Event
+                {
+                    level = msg.level.ToString(),
+                    message = msg.message,
+                    metadata = msg.metadata,
+
+                    @namespace = message?.@event?.@namespace,
+                    app = message?.@event?.app
+                }
             };
 
             var request = new HttpRequestMessage(HttpMethod.Post, url);
@@ -26,18 +54,67 @@ namespace Vit.Core.Module.Log.LogCollector
 
             // TODO:    retry when fail. 
             //          batch:  batchIntervalInSeconds, batchSizeLimit, queueLimit
-            var response = httpClient.SendAsync(request);
+            httpClient.SendAsync(request);
+
+            //var strMsg = msgBody.Serialize();
+            //var response = httpClient.SendAsync(request).Result;
+
         }
 
+
+
         public class Message 
-        {
-            /// <summary>
-            /// 
-            /// </summary>
+        {     
+            public double time;
+
+            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
             public string index;
 
-            public object source;
+            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+            public string host;
+
+            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+            public string source;
+
+            [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+            public string sourcetype;
+
+            [JsonProperty("event")]
+            public Event @event;
+
+            public 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 string @namespace;
+
+                [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
+                public string app;
+            }
+
+            /*
+             {
+                "time": 1426279439,  
+                "host": "localhost",
+                "source": "random-data-generator",
+                "sourcetype": "my_sample_data",
+                "index": "index_prod",
+                "event": { 
+                    "message": "Something happened",
+                    "severity": "INFO"
+                }
+            }
+             */
         }
         public Message message;
 

+ 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> metadata;
+        public Object[] metadata;
     }
 }

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

@@ -17,7 +17,7 @@ namespace Vit.Core.Module.Log
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Debug(string message, params object[] metadata)
         {
-            Log(Level.DEBUG, message, metadata);
+            Log(Level.debug, message, metadata);
         }
 
         /// <summary>
@@ -28,7 +28,7 @@ namespace Vit.Core.Module.Log
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Info(string message, params object[] metadata)
         {
-            Log(Level.INFO, message, metadata);
+            Log(Level.info, message, metadata);
         }
 
 
@@ -40,14 +40,14 @@ namespace Vit.Core.Module.Log
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Error(string message, params object[] metadata)
         {
-            Log(Level.ERROR, message,metadata);
+            Log(Level.error, message,metadata);
         }
 
 
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void Warn(string message, params object[] metadata)
         {
-            Log(Level.WARN, message, metadata);
+            Log(Level.warn, message, metadata);
         }
 
 

+ 1 - 1
dotnet/ServiceCenter/Sers.ServiceCenter/Apm/Sers.Gover.Apm.Zipkin/AppEvent.cs

@@ -322,7 +322,7 @@ namespace Sers.Gover.Apm.Zipkin
         [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
         public void LogWarning(string message)
         {
-            Logger.log.Log(Level.WARN, "[zipkin]" + message);
+            Logger.log.Log(Level.warn, "[zipkin]" + message);
         }
     }
     #endregion