From a45f7cbd656a5109c41355856bb82e347f864376 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Wed, 18 Oct 2023 13:48:42 +0200 Subject: [PATCH] Updated --- EonaCat.Logger/Managers/LogHelper.cs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/EonaCat.Logger/Managers/LogHelper.cs b/EonaCat.Logger/Managers/LogHelper.cs index 59049e6..31f0607 100644 --- a/EonaCat.Logger/Managers/LogHelper.cs +++ b/EonaCat.Logger/Managers/LogHelper.cs @@ -5,6 +5,7 @@ using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; using EonaCat.Json; using EonaCat.Logger.Extensions; using EonaCat.Logger.GrayLog; @@ -135,9 +136,9 @@ namespace EonaCat.Logger.Managers logger.Log(logLevel, message); } - public static async Task SendToSplunkServersAsync(LoggerSettings settings, string logType, string message, bool sendToSplunkServer) + public static async Task SendToSplunkServersAsync(LoggerSettings settings, string jsonPayload, bool sendToSplunkServer) { - if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(message)) + if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(jsonPayload)) return; if (settings.SplunkServers == null) @@ -166,15 +167,7 @@ namespace EonaCat.Logger.Managers { using (var httpClient = new HttpClient(splunkServer.SplunkClientHandler)) { - var payload = new - { - log_message = message, - sourcetype = logType - }; - - var jsonPayload = JsonHelper.ToJson(payload); var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); - httpClient.DefaultRequestHeaders.Add("Authorization", $"Splunk {splunkServer.SplunkHecToken}"); var response = await httpClient.PostAsync(splunkServer.SplunkHecUrl, content); @@ -193,6 +186,19 @@ namespace EonaCat.Logger.Managers } } + public static async Task SendToSplunkServersAsync(LoggerSettings settings, string logType, string message, bool sendToSplunkServer) + { + if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(message)) + return; + + var jsonPayload = JsonHelper.ToJson(new + { + @event = message, + sourcetype = logType + }); + await SendToSplunkServersAsync(settings, logType, jsonPayload, sendToSplunkServer); + } + internal static async Task SendToGrayLogServersAsync(LoggerSettings settings, string message, ELogType logLevel, string facility, string source, bool sendToGrayLogServer, string version = "1.1") { if (settings == null || !sendToGrayLogServer || string.IsNullOrWhiteSpace(message))