lith 3 yıl önce
ebeveyn
işleme
85b2429d03

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

@@ -1,5 +1,7 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using System;
+using System.Threading;
+
 using Vit.Core.Module.Log;
 using Vit.Core.Util.ComponentModel.SsError;
 
@@ -23,8 +25,8 @@ namespace Vit.Core.MsTest.Module
             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" });
 
-                                        
 
+            Thread.Sleep(3000);
         }
     }
 }

+ 4 - 2
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/appsettings.json

@@ -22,8 +22,10 @@
           "className": "Vit.Core.Module.Log.LogCollector.Splunk.SplunkCollector",
 
           "client": {
-            "url": "http://192.168.20.20:8088/services/collector",
-            "authToken": "xxxxx"
+            "url": "https://192.168.20.20:8088/services/collector",
+            "authToken": "xxxxx",
+            //若指定则在指定时间间隔统一推送数据,若不指定则立即推送。单位:ms
+            "//intervalMs": 2000
           },
 
           "//message": {

+ 70 - 5
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/Splunk/SplunkClient.cs

@@ -3,6 +3,7 @@ using System.Net.Http.Headers;
 using System.Net.Http;
 
 using Vit.Extensions;
+using System.Collections.Concurrent;
 
 namespace Vit.Core.Module.Log.LogCollector.Splunk
 {
@@ -16,6 +17,10 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
 
         public string authToken;
 
+        /// <summary>
+        /// 若指定则在指定时间间隔统一推送数据,若不指定则立即推送。单位:ms
+        /// </summary>
+        public int? intervalMs;
 
         /// <summary>
         /// 默认 "Splunk"
@@ -28,22 +33,82 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
 
 
 
-       
+
         public void Init()
         {
-            httpClient = new System.Net.Http.HttpClient();
+            //信任所有的证书
+            var HttpHandler = new HttpClientHandler
+            {
+                ServerCertificateCustomValidationCallback = (a, b, c, d) => true
+            };
+            httpClient = new System.Net.Http.HttpClient(HttpHandler);
             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());
             }
+
+            InitTimer();
         }
 
-        private System.Net.Http.HttpClient httpClient = null;
 
 
 
-        public void SendAsync(SplunkRecord record) 
+        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
+        public void SendAsync(SplunkRecord record)
+        {
+            if (recordList == null)
+                SendToServer(record);
+            else
+                recordList.Add(record);
+        }
+
+
+        ~SplunkClient()
+        {
+            if (time != null)
+            {
+                time?.Stop();
+                time = null;
+            }
+        }
+
+
+        #region Timer
+        ConcurrentBag<SplunkRecord> recordList;
+        ConcurrentBag<SplunkRecord> recordList_Swap;
+        Util.Threading.Timer.SersTimer_SingleThread time;
+
+        private void InitTimer()
+        {
+            if (intervalMs.HasValue && intervalMs.Value > 0)
+            {
+                recordList = new ConcurrentBag<SplunkRecord>();
+                recordList_Swap = new ConcurrentBag<SplunkRecord>();
+                time = new Util.Threading.Timer.SersTimer_SingleThread();
+                time.intervalMs = intervalMs.Value;
+                time.timerCallback = (e) =>
+                {
+                    (recordList_Swap, recordList) = (recordList, recordList_Swap);
+                    if (recordList_Swap.Count > 0)
+                    {
+                        SendToServer(recordList_Swap);
+                        while (recordList_Swap.TryTake(out _)) ;
+                    }
+                };
+                time.Start();
+            }
+        }
+        #endregion
+
+
+
+
+
+        private System.Net.Http.HttpClient httpClient = null;
+
+        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
+        private void SendToServer(object record) 
         {
             var request = new HttpRequestMessage(HttpMethod.Post, url);
             request.Content = new StringContent(record.Serialize(), Vit.Core.Module.Serialization.Serialization_Newtonsoft.defaultEncoding, "application/json");
@@ -52,7 +117,7 @@ namespace Vit.Core.Module.Log.LogCollector.Splunk
             //          batch:  batchIntervalInSeconds, batchSizeLimit, queueLimit
             httpClient.SendAsync(request);
 
-            //var strMsg = msgBody.Serialize();
+            //var strMsg = record.Serialize();
             //var response = httpClient.SendAsync(request).Result;
         }
     }

+ 4 - 2
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/LogCollector/Vit.Logger.appsettings.json

@@ -19,8 +19,10 @@
           "className": "Vit.Core.Module.Log.LogCollector.Splunk.SplunkCollector",
 
           "client": {
-            "url": "http://192.168.20.20:8088/services/collector",
-            "authToken": "xxxxx"
+            "url": "https://192.168.20.20:8088/services/collector",
+            "authToken": "xxxxx",
+            //若指定则在指定时间间隔统一推送数据,若不指定则立即推送。单位:ms
+            "//intervalMs": 2000
           },
 
           "//message": {