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

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

View File

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

View File

@@ -44,6 +44,11 @@ namespace EonaCat.Logger.Servers.Zabbix
/// <exception cref="TimeoutException"></exception>
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));
using (TcpClient tcpClient = new TcpClient(server, port))
using (NetworkStream stream = tcpClient.GetStream())