Updated
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
net7.0;
|
net7.0;
|
||||||
</TargetFrameworks>
|
</TargetFrameworks>
|
||||||
<ApplicationIcon>icon.ico</ApplicationIcon>
|
<ApplicationIcon>icon.ico</ApplicationIcon>
|
||||||
<Version>1.2.1</Version>
|
<Version>1.2.2</Version>
|
||||||
<Authors>EonaCat (Jeroen Saey)</Authors>
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Company>EonaCat (Jeroen Saey)</Company>
|
<Company>EonaCat (Jeroen Saey)</Company>
|
||||||
|
|||||||
@@ -16,8 +16,16 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace EonaCat.Logger.Managers
|
namespace EonaCat.Logger.Managers
|
||||||
{
|
{
|
||||||
|
public class ErrorMessage
|
||||||
|
{
|
||||||
|
public Exception Exception { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
internal static class LogHelper
|
internal static class LogHelper
|
||||||
{
|
{
|
||||||
|
internal static event EventHandler<ErrorMessage> OnException;
|
||||||
|
|
||||||
internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(currentMessage))
|
if (string.IsNullOrWhiteSpace(currentMessage))
|
||||||
@@ -147,6 +155,7 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
if (!splunkServer.HasHecToken)
|
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}'");
|
Console.WriteLine($"Splunk server HecToken not specified, skipping splunkServer '{splunkServer.SplunkHecUrl}'");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -172,13 +181,13 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
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)
|
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)
|
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))
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (server.Port < 0)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
server.Udp.Send(data, data.Length);
|
server.Udp.Send(data, data.Length);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
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}" });
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
public partial class LogManager : ILogManager, IDisposable
|
public partial class LogManager : ILogManager, IDisposable
|
||||||
{
|
{
|
||||||
|
public event EventHandler<ErrorMessage> OnException;
|
||||||
private DateTime CurrentDateTme => Settings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
|
private DateTime CurrentDateTme => Settings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
|
||||||
private DateTime _logDate;
|
private DateTime _logDate;
|
||||||
public ILoggerProvider LoggerProvider { get; private set; }
|
public ILoggerProvider LoggerProvider { get; private set; }
|
||||||
@@ -178,6 +179,12 @@ namespace EonaCat.Logger.Managers
|
|||||||
Settings = settings;
|
Settings = settings;
|
||||||
SetupFileLogger(settings, null, true);
|
SetupFileLogger(settings, null, true);
|
||||||
SetupLogManager();
|
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)
|
private void SetupFileLogger(LoggerSettings settings = null, string logFolder = null, bool defaultPrefix = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user