Updated
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
<TargetFrameworks>.netstandard2.1; net6.0; net7.0; net8.0; net4.8;</TargetFrameworks>
|
<TargetFrameworks>.netstandard2.1; net6.0; net7.0; net8.0; net4.8;</TargetFrameworks>
|
||||||
<ApplicationIcon>icon.ico</ApplicationIcon>
|
<ApplicationIcon>icon.ico</ApplicationIcon>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<FileVersion>1.3.4</FileVersion>
|
<FileVersion>1.3.5</FileVersion>
|
||||||
<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>
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<EVRevisionFormat>1.3.4+{chash:10}.{c:ymd}</EVRevisionFormat>
|
<EVRevisionFormat>1.3.5+{chash:10}.{c:ymd}</EVRevisionFormat>
|
||||||
<EVDefault>true</EVDefault>
|
<EVDefault>true</EVDefault>
|
||||||
<EVInfo>true</EVInfo>
|
<EVInfo>true</EVInfo>
|
||||||
<EVTagMatch>v[0-9]*</EVTagMatch>
|
<EVTagMatch>v[0-9]*</EVTagMatch>
|
||||||
|
|||||||
@@ -75,8 +75,10 @@ public class GrayLogServer
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// IP:port of the server.
|
/// IP:port of the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string IpPort => _Hostname + ":" + _Port;
|
public string IpPort => _Hostname + ":" + _Port;
|
||||||
|
|
||||||
|
public bool SupportsTcp { get; set; }
|
||||||
|
|
||||||
private void SetUdp()
|
private void SetUdp()
|
||||||
{
|
{
|
||||||
Udp = null;
|
Udp = null;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using EonaCat.Json;
|
using EonaCat.Json;
|
||||||
@@ -144,33 +145,36 @@ internal static class LogHelper
|
|||||||
|
|
||||||
public static async Task SendToSplunkServersAsync(LoggerSettings settings, SplunkPayload splunkPayload, bool sendToSplunkServer)
|
public static async Task SendToSplunkServersAsync(LoggerSettings settings, SplunkPayload splunkPayload, bool sendToSplunkServer)
|
||||||
{
|
{
|
||||||
if (settings == null || !sendToSplunkServer || splunkPayload == null) return;
|
if (settings == null || !sendToSplunkServer || splunkPayload == null)
|
||||||
|
|
||||||
foreach (var splunkServer in settings.SplunkServers ?? Enumerable.Empty<SplunkServer.SplunkServer>())
|
|
||||||
{
|
{
|
||||||
if (!splunkServer.HasHecUrl || !splunkServer.HasHecToken)
|
return;
|
||||||
{
|
|
||||||
OnException?.Invoke(null, new ErrorMessage { Message = $"Invalid Splunk server configuration for '{splunkServer.SplunkHecUrl}'" });
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var response = await splunkServer.SendAsync(splunkPayload);
|
|
||||||
if (!response.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
OnException?.Invoke(null, new ErrorMessage { Message = $"Failed to send log to Splunk '{splunkServer.SplunkHecUrl}'. Status code: {response.StatusCode}" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
OnException?.Invoke(null, new ErrorMessage { Exception = ex, Message = $"Error logging to Splunk '{splunkServer.SplunkHecUrl}': {ex.Message}" });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tasks = settings.SplunkServers?
|
||||||
|
.Where(splunkServer => splunkServer.HasHecUrl && splunkServer.HasHecToken)
|
||||||
|
.Select(async splunkServer =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await splunkServer.SendAsync(splunkPayload);
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
LogError($"Failed to send log to Splunk '{splunkServer.SplunkHecUrl}'. Status code: {response.StatusCode}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogError($"Error logging to Splunk '{splunkServer.SplunkHecUrl}': {ex.Message}", ex);
|
||||||
|
}
|
||||||
|
}) ?? new List<Task>();
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task SendToSplunkServersAsync(LoggerSettings settings, string logType, string message,
|
/// <summary>
|
||||||
bool sendToSplunkServer)
|
/// Overload for sending a simple log message to Splunk.
|
||||||
|
/// </summary>
|
||||||
|
public static async Task SendToSplunkServersAsync(LoggerSettings settings, string logType, string message, bool sendToSplunkServer)
|
||||||
{
|
{
|
||||||
if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(message))
|
if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(message))
|
||||||
{
|
{
|
||||||
@@ -187,86 +191,169 @@ internal static class LogHelper
|
|||||||
await SendToSplunkServersAsync(settings, splunkPayload, sendToSplunkServer);
|
await SendToSplunkServersAsync(settings, splunkPayload, sendToSplunkServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logs an error using the OnException event.
|
||||||
|
/// </summary>
|
||||||
|
private static void LogError(string message, Exception ex = null)
|
||||||
|
{
|
||||||
|
OnException?.Invoke(null, new ErrorMessage { Message = message, Exception = ex });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static async Task SendToGrayLogServersAsync(LoggerSettings settings, string message, ELogType logLevel,
|
internal static async Task SendToGrayLogServersAsync(LoggerSettings settings, string message, ELogType logLevel,
|
||||||
string facility, string source, bool sendToGrayLogServer, string version = "1.1")
|
string facility, string source, bool sendToGrayLogServer, string version = "1.1")
|
||||||
{
|
{
|
||||||
if (settings == null || !sendToGrayLogServer || string.IsNullOrWhiteSpace(message))
|
if (settings == null || !sendToGrayLogServer || string.IsNullOrWhiteSpace(message))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var grayLogServer in settings.GrayLogServers ?? new List<GrayLogServer> { new("127.0.0.1") })
|
const int MaxUdpPacketSize = 4096;
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var gelfMessage = new
|
|
||||||
{
|
|
||||||
version,
|
|
||||||
host = MachineName,
|
|
||||||
short_message = message,
|
|
||||||
level = logLevel.ToGrayLogLevel(),
|
|
||||||
facility,
|
|
||||||
source,
|
|
||||||
timestamp = DateTime.UtcNow.ToUnixTimestamp()
|
|
||||||
};
|
|
||||||
|
|
||||||
var messageBytes = Encoding.UTF8.GetBytes(JsonHelper.ToJson(gelfMessage));
|
var gelfMessage = new
|
||||||
await grayLogServer.Udp.SendAsync(messageBytes, messageBytes.Length);
|
{
|
||||||
}
|
version,
|
||||||
catch (Exception exception)
|
host = MachineName,
|
||||||
|
short_message = message,
|
||||||
|
level = logLevel.ToGrayLogLevel(),
|
||||||
|
facility,
|
||||||
|
source,
|
||||||
|
timestamp = DateTime.UtcNow.ToUnixTimestamp()
|
||||||
|
};
|
||||||
|
|
||||||
|
var messageBytes = Encoding.UTF8.GetBytes(JsonHelper.ToJson(gelfMessage));
|
||||||
|
|
||||||
|
var tasks = settings.GrayLogServers?
|
||||||
|
.Where(server => !string.IsNullOrWhiteSpace(server.Hostname) && server.Port >= 0)
|
||||||
|
.Select(async grayLogServer =>
|
||||||
{
|
{
|
||||||
OnException?.Invoke(null,
|
try
|
||||||
new ErrorMessage
|
{
|
||||||
|
if (messageBytes.Length <= MaxUdpPacketSize)
|
||||||
{
|
{
|
||||||
Exception = exception,
|
// Send via UDP (single packet)
|
||||||
Message =
|
await grayLogServer.Udp.SendAsync(messageBytes, messageBytes.Length);
|
||||||
$"Error while logging to GrayLog Server '{grayLogServer.Hostname}': {exception.Message}"
|
}
|
||||||
|
else if (grayLogServer.SupportsTcp)
|
||||||
|
{
|
||||||
|
// Send via TCP if supported
|
||||||
|
await SendViaTcpAsync(grayLogServer, messageBytes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Chunk large messages for UDP
|
||||||
|
await SendUdpInChunksAsync(grayLogServer, messageBytes, MaxUdpPacketSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
OnException?.Invoke(null, new ErrorMessage
|
||||||
|
{
|
||||||
|
Exception = ex,
|
||||||
|
Message = $"Error logging to GrayLog Server '{grayLogServer.Hostname}': {ex.Message}"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}) ?? new List<Task>();
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a message via TCP to a GrayLog server.
|
||||||
|
/// </summary>
|
||||||
|
private static async Task SendViaTcpAsync(GrayLogServer server, byte[] data)
|
||||||
|
{
|
||||||
|
using var tcpClient = new TcpClient();
|
||||||
|
await tcpClient.ConnectAsync(server.Hostname, server.Port);
|
||||||
|
using var stream = tcpClient.GetStream();
|
||||||
|
await stream.WriteAsync(data, 0, data.Length);
|
||||||
|
await stream.FlushAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends large messages in chunks over UDP.
|
||||||
|
/// </summary>
|
||||||
|
private static async Task SendUdpInChunksAsync(GrayLogServer server, byte[] data, int chunkSize)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < data.Length; i += chunkSize)
|
||||||
|
{
|
||||||
|
var chunk = data.Skip(i).Take(chunkSize).ToArray();
|
||||||
|
await server.Udp.SendAsync(chunk, chunk.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task SendToSysLogServersAsync(LoggerSettings settings, string message,
|
internal static async Task SendToSysLogServersAsync(LoggerSettings settings, string message, bool sendToSyslogServers)
|
||||||
bool sendToSyslogServers)
|
|
||||||
{
|
{
|
||||||
if (settings == null || !sendToSyslogServers || string.IsNullOrWhiteSpace(message))
|
if (settings == null || !sendToSyslogServers || string.IsNullOrWhiteSpace(message))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var server in settings.SysLogServers ?? new List<SyslogServer> { new("127.0.0.1") })
|
const int MaxUdpPacketSize = 4096;
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(server.Hostname))
|
|
||||||
{
|
|
||||||
OnException?.Invoke(null,
|
|
||||||
new ErrorMessage { Message = "Server hostname not specified, skipping SysLog Server" });
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (server.Port < 0)
|
var tasks = settings.SysLogServers?
|
||||||
{
|
.Where(server => !string.IsNullOrWhiteSpace(server.Hostname) && server.Port >= 0)
|
||||||
OnException?.Invoke(null,
|
.Select(async server =>
|
||||||
new ErrorMessage { Message = "Server port must be zero or greater, skipping SysLog Server" });
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var data = Encoding.UTF8.GetBytes(message);
|
|
||||||
await server.Udp.SendAsync(data, data.Length);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
{
|
||||||
OnException?.Invoke(null,
|
try
|
||||||
new ErrorMessage
|
{
|
||||||
|
var data = Encoding.UTF8.GetBytes(message);
|
||||||
|
|
||||||
|
if (data.Length <= MaxUdpPacketSize)
|
||||||
{
|
{
|
||||||
Exception = exception,
|
// Send via UDP (single packet)
|
||||||
Message = $"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}"
|
await server.Udp.SendAsync(data, data.Length);
|
||||||
|
}
|
||||||
|
else if (server.SupportsTcp)
|
||||||
|
{
|
||||||
|
// Send via TCP if supported
|
||||||
|
await SendViaTcpAsync(server, data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Chunk large messages for UDP
|
||||||
|
await SendUdpInChunksAsync(server, data, MaxUdpPacketSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
OnException?.Invoke(null, new ErrorMessage
|
||||||
|
{
|
||||||
|
Exception = ex,
|
||||||
|
Message = $"Error logging to SysLog Server '{server.Hostname}': {ex.Message}"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}) ?? new List<Task>();
|
||||||
|
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a message via TCP to a syslog server.
|
||||||
|
/// </summary>
|
||||||
|
private static async Task SendViaTcpAsync(SyslogServer server, byte[] data)
|
||||||
|
{
|
||||||
|
using var tcpClient = new TcpClient();
|
||||||
|
await tcpClient.ConnectAsync(server.Hostname, server.Port);
|
||||||
|
using var stream = tcpClient.GetStream();
|
||||||
|
await stream.WriteAsync(data, 0, data.Length);
|
||||||
|
await stream.FlushAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends large messages in chunks over UDP.
|
||||||
|
/// </summary>
|
||||||
|
private static async Task SendUdpInChunksAsync(SyslogServer server, byte[] data, int chunkSize)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < data.Length; i += chunkSize)
|
||||||
|
{
|
||||||
|
var chunk = data.Skip(i).Take(chunkSize).ToArray();
|
||||||
|
await server.Udp.SendAsync(chunk, chunk.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static string GetStartupMessage()
|
internal static string GetStartupMessage()
|
||||||
{
|
{
|
||||||
return $"{DllInfo.ApplicationName} started.";
|
return $"{DllInfo.ApplicationName} started.";
|
||||||
|
|||||||
@@ -75,8 +75,10 @@ public class SyslogServer
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// IP:port of the server.
|
/// IP:port of the server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string IpPort => _Hostname + ":" + _Port;
|
public string IpPort => _Hostname + ":" + _Port;
|
||||||
|
|
||||||
|
public bool SupportsTcp { get; set; }
|
||||||
|
|
||||||
private void SetUdp()
|
private void SetUdp()
|
||||||
{
|
{
|
||||||
Udp = null;
|
Udp = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user