This commit is contained in:
Jeroen
2023-10-18 12:58:32 +02:00
parent b54d1edac0
commit 298a350b2f
3 changed files with 24 additions and 15 deletions

View File

@@ -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<ErrorMessage> 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}" });
}
});
}