This commit is contained in:
Jeroen Saey
2025-02-28 13:42:17 +01:00
parent 981c86c59e
commit d7944065a6
5 changed files with 40 additions and 12 deletions

View File

@@ -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.4.4</FileVersion> <FileVersion>1.4.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.4.4+{chash:10}.{c:ymd}</EVRevisionFormat> <EVRevisionFormat>1.4.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>

View File

@@ -259,7 +259,7 @@ public static class LogHelper
return; return;
} }
var isEnabled = (splunkServer?.TypesToLog == null || splunkServer.TypesToLog.Count == 0 || splunkServer.TypesToLog.Contains(currentLogType)); var isEnabled = (splunkServer?.TypesToLog == null || splunkServer?.TypesToLog.Count == 0 || splunkServer.TypesToLog.Contains(currentLogType));
if (!isEnabled) if (!isEnabled)
{ {
splunkServer?.DisposeHttpClient(); splunkServer?.DisposeHttpClient();
@@ -267,15 +267,15 @@ public static class LogHelper
} }
} }
var response = await splunkServer.SendAsync(splunkPayload, disableSplunkSSL); var response = await splunkServer?.SendAsync(splunkPayload, disableSplunkSSL);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
LogError($"Failed to send log to Splunk '{splunkServer.SplunkHecUrl}'. Status code: {response.StatusCode}"); LogError($"Failed to send log to Splunk '{splunkServer?.SplunkHecUrl}'. Status code: {response.StatusCode}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
LogError($"Error logging to Splunk '{splunkServer.SplunkHecUrl}': {ex.Message}", ex); LogError($"Error logging to Splunk '{splunkServer?.SplunkHecUrl}': {ex.Message}", ex);
} }
}) ?? new List<Task>(); }) ?? new List<Task>();
@@ -365,13 +365,13 @@ public static class LogHelper
if (!grayLogServer.IsConnected) if (!grayLogServer.IsConnected)
{ {
grayLogServer.SetUdp(); grayLogServer?.SetUdp();
} }
if (messageBytes.Length <= MaxUdpPacketSize) if (messageBytes.Length <= MaxUdpPacketSize)
{ {
// Send via UDP (single packet) // Send via UDP (single packet)
await grayLogServer.Udp.SendAsync(messageBytes, messageBytes.Length); await grayLogServer?.Udp.SendAsync(messageBytes, messageBytes.Length);
} }
else if (grayLogServer.SupportsTcp) else if (grayLogServer.SupportsTcp)
{ {
@@ -402,6 +402,10 @@ public static class LogHelper
/// </summary> /// </summary>
private static async Task SendViaTcpAsync(Graylog server, byte[] data) private static async Task SendViaTcpAsync(Graylog server, byte[] data)
{ {
if (server == null)
{
return;
}
using var tcpClient = new System.Net.Sockets.TcpClient(); using var tcpClient = new System.Net.Sockets.TcpClient();
await tcpClient.ConnectAsync(server.Hostname, server.Port); await tcpClient.ConnectAsync(server.Hostname, server.Port);
using var stream = tcpClient.GetStream(); using var stream = tcpClient.GetStream();
@@ -414,6 +418,10 @@ public static class LogHelper
/// </summary> /// </summary>
private static async Task SendUdpInChunksAsync(Graylog server, byte[] data, int chunkSize) private static async Task SendUdpInChunksAsync(Graylog server, byte[] data, int chunkSize)
{ {
if (server == null)
{
return;
}
for (int i = 0; i < data.Length; i += chunkSize) for (int i = 0; i < data.Length; i += chunkSize)
{ {
var chunk = data.Skip(i).Take(chunkSize).ToArray(); var chunk = data.Skip(i).Take(chunkSize).ToArray();

View File

@@ -98,7 +98,7 @@ public class Syslog : IDisposable
try try
{ {
Udp?.Close(); Udp?.Close();
Udp.Dispose(); Udp?.Dispose();
} }
catch catch
{ {
@@ -127,10 +127,20 @@ public class Syslog : IDisposable
private async Task SendAsync(byte[] data) private async Task SendAsync(byte[] data)
{ {
if (data == null)
{
return;
}
if (Udp == null)
{
return;
}
if (data.Length <= MaxUdpPacketSize) if (data.Length <= MaxUdpPacketSize)
{ {
// Send via UDP (single packet) // Send via UDP (single packet)
await Udp.SendAsync(data, data.Length); await Udp?.SendAsync(data, data.Length);
} }
else if (SupportsTcp) else if (SupportsTcp)
{ {
@@ -149,6 +159,11 @@ public class Syslog : IDisposable
/// </summary> /// </summary>
private static async Task SendViaTcpAsync(Servers.Syslog.Syslog server, byte[] data) private static async Task SendViaTcpAsync(Servers.Syslog.Syslog server, byte[] data)
{ {
if (server == null)
{
return;
}
using var tcpClient = new System.Net.Sockets.TcpClient(); using var tcpClient = new System.Net.Sockets.TcpClient();
await tcpClient.ConnectAsync(server.Hostname, server.Port); await tcpClient.ConnectAsync(server.Hostname, server.Port);
using var stream = tcpClient.GetStream(); using var stream = tcpClient.GetStream();

View File

@@ -128,7 +128,7 @@ namespace EonaCat.Logger.Servers.Udp
byte[] chunk = new byte[chunkSize]; byte[] chunk = new byte[chunkSize];
Array.Copy(data, offset, chunk, 0, chunkSize); Array.Copy(data, offset, chunk, 0, chunkSize);
await _udp.SendAsync(chunk, chunk.Length); await _udp?.SendAsync(chunk, chunk.Length);
offset += chunkSize; offset += chunkSize;
} }
} }
@@ -160,7 +160,7 @@ namespace EonaCat.Logger.Servers.Udp
byte[] chunk = new byte[chunkSize]; byte[] chunk = new byte[chunkSize];
Array.Copy(sendData, offset, chunk, 0, chunkSize); Array.Copy(sendData, offset, chunk, 0, chunkSize);
await _udp.SendAsync(chunk, chunk.Length); await _udp?.SendAsync(chunk, chunk.Length);
offset += chunkSize; offset += chunkSize;
} }
} }

View File

@@ -44,6 +44,11 @@ namespace EonaCat.Logger.Servers.Zabbix
/// <exception cref="TimeoutException"></exception> /// <exception cref="TimeoutException"></exception>
public async Task<ZabbixResponse> SendAsync(string server, int port = 10051, int timeout = 500) public async Task<ZabbixResponse> SendAsync(string server, int port = 10051, int timeout = 500)
{ {
if (string.IsNullOrWhiteSpace(server))
{
return new ZabbixResponse();
}
string json = JsonHelper.ToJson(new ZabbixRequest(Data[0].Host, Data[0].Key, Data[0].Value)); string json = JsonHelper.ToJson(new ZabbixRequest(Data[0].Host, Data[0].Key, Data[0].Value));
using (TcpClient tcpClient = new TcpClient(server, port)) using (TcpClient tcpClient = new TcpClient(server, port))
using (NetworkStream stream = tcpClient.GetStream()) using (NetworkStream stream = tcpClient.GetStream())