From 298a350b2ffb3b03be147f58b5ceb4b8fc4c6662 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Wed, 18 Oct 2023 12:58:32 +0200 Subject: [PATCH] Updated --- EonaCat.Logger/EonaCat.Logger.csproj | 2 +- EonaCat.Logger/Managers/LogHelper.cs | 30 ++++++++++++++------------- EonaCat.Logger/Managers/LogManager.cs | 7 +++++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj index 41da821..d71afe3 100644 --- a/EonaCat.Logger/EonaCat.Logger.csproj +++ b/EonaCat.Logger/EonaCat.Logger.csproj @@ -7,7 +7,7 @@ net7.0; icon.ico - 1.2.1 + 1.2.2 EonaCat (Jeroen Saey) true EonaCat (Jeroen Saey) diff --git a/EonaCat.Logger/Managers/LogHelper.cs b/EonaCat.Logger/Managers/LogHelper.cs index 3fd3901..857f06a 100644 --- a/EonaCat.Logger/Managers/LogHelper.cs +++ b/EonaCat.Logger/Managers/LogHelper.cs @@ -16,8 +16,16 @@ using Microsoft.Extensions.Logging; namespace EonaCat.Logger.Managers { + public class ErrorMessage + { + public Exception Exception { get; set; } + public string Message { get; set; } + } + internal static class LogHelper { + internal static event EventHandler OnException; + internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime) { if (string.IsNullOrWhiteSpace(currentMessage)) @@ -147,6 +155,7 @@ namespace EonaCat.Logger.Managers if (!splunkServer.HasHecToken) { + OnException?.Invoke(null, new ErrorMessage { Message = $"Splunk server HecToken not specified, skipping splunkServer '{splunkServer.SplunkHecUrl}'" }); Console.WriteLine($"Splunk server HecToken not specified, skipping splunkServer '{splunkServer.SplunkHecUrl}'"); continue; } @@ -172,13 +181,13 @@ namespace EonaCat.Logger.Managers if (!response.IsSuccessStatusCode) { - Console.WriteLine($"Failed to send log to Splunk '{splunkServer.SplunkHecUrl}'. Status code: {response.StatusCode}"); + OnException?.Invoke(null, new ErrorMessage { Message = $"Failed to send log to Splunk '{splunkServer.SplunkHecUrl}'. Status code: {response.StatusCode}" }); } } } catch (Exception exception) { - Console.WriteLine($"Error while logging to Splunk server '{splunkServer.SplunkHecUrl}': {exception.Message}"); + OnException?.Invoke(null, new ErrorMessage { Exception = exception, Message = $"Error while logging to Splunk Server '{splunkServer.SplunkHecUrl}': {exception.Message}" }); } }); } @@ -216,7 +225,7 @@ namespace EonaCat.Logger.Managers } catch (Exception exception) { - Console.WriteLine($"Error while logging to GrayLog server '{grayLogServer.Hostname}': {exception.Message}"); + OnException?.Invoke(null, new ErrorMessage { Exception = exception, Message = $"Error while logging to GrayLog Server '{grayLogServer.Hostname}': {exception.Message}" }); } }); } @@ -242,28 +251,21 @@ namespace EonaCat.Logger.Managers { if (string.IsNullOrWhiteSpace(server.Hostname)) { - Console.WriteLine("Server hostname not specified, skipping SysLog Server"); + OnException?.Invoke(null, new ErrorMessage { Message = "Server hostname not specified, skipping SysLog Server" }); return; } if (server.Port < 0) { - Console.WriteLine("Server port must be zero or greater, skipping SysLog Server"); + OnException?.Invoke(null, new ErrorMessage { Message = "Server port must be zero or greater, skipping SysLog Server" }); return; } - try - { - server.Udp.Send(data, data.Length); - } - catch (Exception exception) - { - Console.WriteLine($"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}"); - } + server.Udp.Send(data, data.Length); } catch (Exception exception) { - Console.WriteLine($"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}"); + OnException?.Invoke(null, new ErrorMessage { Exception = exception, Message = $"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}" }); } }); } diff --git a/EonaCat.Logger/Managers/LogManager.cs b/EonaCat.Logger/Managers/LogManager.cs index be506ac..3b59ed8 100644 --- a/EonaCat.Logger/Managers/LogManager.cs +++ b/EonaCat.Logger/Managers/LogManager.cs @@ -18,6 +18,7 @@ namespace EonaCat.Logger.Managers public partial class LogManager : ILogManager, IDisposable { + public event EventHandler OnException; private DateTime CurrentDateTme => Settings.UseLocalTime ? DateTime.Now : DateTime.UtcNow; private DateTime _logDate; public ILoggerProvider LoggerProvider { get; private set; } @@ -178,6 +179,12 @@ namespace EonaCat.Logger.Managers Settings = settings; SetupFileLogger(settings, null, true); SetupLogManager(); + LogHelper.OnException += LogHelper_OnException; + } + + private void LogHelper_OnException(object sender, ErrorMessage e) + { + OnException?.Invoke(sender, e); } private void SetupFileLogger(LoggerSettings settings = null, string logFolder = null, bool defaultPrefix = true)