Added Splunk Logging and GrayLog Logging

This commit is contained in:
Jeroen
2023-10-17 22:42:54 +02:00
parent b69b4abf62
commit b54d1edac0
11 changed files with 412 additions and 29 deletions

View File

@@ -67,6 +67,31 @@ namespace EonaCat.Logger
}
}
public static string ToString(this ELogType logLevel)
{
switch (logLevel)
{
case ELogType.NONE:
return "NONE";
case ELogType.ERROR:
return "ERROR";
case ELogType.DEBUG:
return "DEBUG";
case ELogType.CRITICAL:
return "CRITICAL";
case ELogType.WARNING:
return "WARNING";
case ELogType.TRACE:
return "TRACE";
case ELogType.TRAFFIC:
return "TRAFFIC";
case ELogType.INFO:
return "INFO";
default:
return "INFO";
}
}
public static ELogType FromSeverity(this ESeverity logLevel)
{
switch (logLevel)
@@ -88,6 +113,22 @@ namespace EonaCat.Logger
}
}
public static int ToGrayLogLevel(this ELogType logLevel)
{
// Loglevel to GELF format
switch (logLevel.ToString())
{
case "TRAFFIC": return 7;
case "TRACE": return 7;
case "DEBUG": return 7;
case "INFO": return 6;
case "WARNING": return 4;
case "ERROR": return 3;
case "CRITICAL": return 2;
default: return 6; // Default to INFO
}
}
public static ESeverity ToSeverity(this ELogType logLevel)
{
switch (logLevel)

View File

@@ -7,7 +7,7 @@
net7.0;
</TargetFrameworks>
<ApplicationIcon>icon.ico</ApplicationIcon>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<Authors>EonaCat (Jeroen Saey)</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Company>EonaCat (Jeroen Saey)</Company>
@@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="EonaCat.Json" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />

View File

@@ -0,0 +1,12 @@
using System;
namespace EonaCat.Logger.Extensions
{
public static class DateTimeExtensions
{
public static long ToUnixTimestamp(this DateTime dateTime)
{
return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using System.Net.Sockets;
namespace EonaCat.Logger.GrayLog
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
/// <summary>
/// Syslog server.
/// </summary>
public class GrayLogServer
{
/// <summary>
/// Hostname.
/// </summary>
public string Hostname
{
get
{
return _Hostname;
}
set
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(Hostname));
_Hostname = value;
SetUdp();
}
}
/// <summary>
/// UDP port.
/// </summary>
public int Port
{
get
{
return _Port;
}
set
{
if (value < 0) throw new ArgumentException("Port must be zero or greater.");
_Port = value;
SetUdp();
}
}
/// <summary>
/// IP:port of the server.
/// </summary>
public string IpPort
{
get
{
return _Hostname + ":" + _Port;
}
}
internal readonly object SendLock = new object();
internal UdpClient Udp = null;
private string _Hostname = "127.0.0.1";
private int _Port = 12201;
/// <summary>
/// Instantiate the object.
/// </summary>
public GrayLogServer()
{
}
/// <summary>
/// Instantiate the object.
/// </summary>
/// <param name="hostname">Hostname.</param>
/// <param name="port">Port.</param>
public GrayLogServer(string hostname = "127.0.0.1", int port = 12201)
{
Hostname = hostname;
Port = port;
}
private void SetUdp()
{
Udp = null;
Udp = new UdpClient(_Hostname, _Port);
}
}
}

View File

@@ -2,6 +2,9 @@
namespace EonaCat.Logger.Managers
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
/// <summary>
/// Colors to use when writing to the console.
/// </summary>

View File

@@ -3,11 +3,14 @@ using System.Threading.Tasks;
namespace EonaCat.Logger.Managers
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
public interface ILogManager
{
void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null);
void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1");
void Write(Exception exception, string module = null, string method = null, bool criticalException = false,
bool? writeToConsole = null);
bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1");
}
}

View File

@@ -2,10 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using EonaCat.Json;
using EonaCat.Logger.Extensions;
using EonaCat.Logger.GrayLog;
using EonaCat.Logger.Syslog;
using Microsoft.Extensions.Logging;
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
namespace EonaCat.Logger.Managers
{
internal static class LogHelper
@@ -43,7 +51,7 @@ namespace EonaCat.Logger.Managers
return sb.ToString();
}
internal static void SendToConsole(LoggerSettings settings, ELogType logType, string message, bool writeToConsole)
{
@@ -119,9 +127,104 @@ namespace EonaCat.Logger.Managers
logger.Log(logLevel, message);
}
internal static void SendToSysLogServers(LoggerSettings settings, string message)
public static async Task SendToSplunkServersAsync(LoggerSettings settings, string logType, string message, bool sendToSplunkServer)
{
if (settings == null || !settings.SendToSyslogServers || string.IsNullOrWhiteSpace(message))
if (settings == null || !sendToSplunkServer || string.IsNullOrWhiteSpace(message))
return;
if (settings.SplunkServers == null)
{
settings.SplunkServers = new List<SplunkServer.SplunkServer>();
}
foreach (var splunkServer in settings.SplunkServers)
{
if (!splunkServer.HasHecUrl)
{
Console.WriteLine("Splunk server HecUrl not specified, skipping splunkServer");
continue;
}
if (!splunkServer.HasHecToken)
{
Console.WriteLine($"Splunk server HecToken not specified, skipping splunkServer '{splunkServer.SplunkHecUrl}'");
continue;
}
await Task.Run(async () =>
{
try
{
using (var httpClient = new HttpClient())
{
var payload = new
{
log_message = message,
sourcetype = logType
};
var jsonPayload = JsonHelper.ToJson(payload);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Add("Authorization", $"Splunk {splunkServer.SplunkHecToken}");
var response = await httpClient.PostAsync(splunkServer.SplunkHecUrl, content);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"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}");
}
});
}
}
internal static async Task SendToGrayLogServersAsync(LoggerSettings settings, string message, ELogType logLevel, string facility, string source, bool sendToGrayLogServer, string version = "1.1")
{
if (settings == null || !sendToGrayLogServer || string.IsNullOrWhiteSpace(message))
return;
if (settings.GrayLogServers == null || !settings.GrayLogServers.Any())
{
settings.GrayLogServers = new List<GrayLogServer> { new GrayLogServer("127.0.0.1", 12201) };
}
foreach (var grayLogServer in settings.GrayLogServers)
{
await Task.Run(async () =>
{
try
{
var gelfMessage = new
{
version,
host = Environment.MachineName,
short_message = message,
level = logLevel.ToGrayLogLevel(),
facility,
source,
timestamp = DateTime.UtcNow.ToUnixTimestamp(),
};
var messageBytes = Encoding.UTF8.GetBytes(JsonHelper.ToJson(gelfMessage));
await grayLogServer.Udp.SendAsync(messageBytes, messageBytes.Length, new IPEndPoint(IPAddress.Parse(grayLogServer.Hostname), grayLogServer.Port));
}
catch (Exception exception)
{
Console.WriteLine($"Error while logging to GrayLog server '{grayLogServer.Hostname}': {exception.Message}");
}
});
}
}
internal static async Task SendToSysLogServersAsync(LoggerSettings settings, string message, bool sendToSyslogServers)
{
if (settings == null || !sendToSyslogServers || string.IsNullOrWhiteSpace(message))
return;
if (settings.SysLogServers == null || !settings.SysLogServers.Any())
@@ -133,28 +236,36 @@ namespace EonaCat.Logger.Managers
foreach (SyslogServer server in settings.SysLogServers)
{
if (string.IsNullOrWhiteSpace(server.Hostname))
{
Console.WriteLine("Server hostname not specified, skipping syslog server");
continue;
}
if (server.Port < 0)
{
Console.WriteLine("Server port must be zero or greater, skipping syslog server");
continue;
}
lock (server.SendLock)
await Task.Run(() =>
{
try
{
server.Udp.Send(data, data.Length);
if (string.IsNullOrWhiteSpace(server.Hostname))
{
Console.WriteLine("Server hostname not specified, skipping SysLog Server");
return;
}
if (server.Port < 0)
{
Console.WriteLine("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}");
}
}
catch
catch (Exception exception)
{
// Do nothing
Console.WriteLine($"Error while logging to SysLog Server '{server.Hostname}': {exception.Message}");
}
}
});
}
}
}

View File

@@ -13,6 +13,9 @@ using EonaCat.Logger.Extensions;
namespace EonaCat.Logger.Managers
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
public partial class LogManager : ILogManager, IDisposable
{
private DateTime CurrentDateTme => Settings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
@@ -98,7 +101,7 @@ namespace EonaCat.Logger.Managers
Logger = LoggerFactory.CreateLogger(Settings.Id);
}
private void InternalWriteAsync(DateTime dateTime, string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
private void InternalWriteAsync(DateTime dateTime, string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null, bool? sendToSyslogServers = null, bool? sendToSplunkServers = null, string customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1")
{
if (string.IsNullOrEmpty(message) || logType == ELogType.NONE || (int)logType >= (int)Settings.MaxLogType)
return;
@@ -113,7 +116,32 @@ namespace EonaCat.Logger.Managers
Task.Run(() => { LogHelper.SendToFile(Logger, Settings, logType, message); });
Task.Run(() => { LogHelper.SendToSysLogServers(Settings, messageWithHeader); });
sendToSyslogServers ??= Settings.SendToSyslogServers;
if (sendToSyslogServers.Value)
{
Task.Run(async () =>
{
await LogHelper.SendToSysLogServersAsync(Settings, messageWithHeader, true);
});
}
sendToSplunkServers ??= Settings.SendToSplunkServers;
if (sendToSplunkServers.Value)
{
Task.Run(async () =>
{
await LogHelper.SendToSplunkServersAsync(Settings, customSplunkSourceType ?? logType.ToString(), messageWithHeader, true);
});
}
sendToGrayLogServers ??= Settings.SendToGrayLogServers;
if (sendToGrayLogServers.Value)
{
Task.Run(async () =>
{
await LogHelper.SendToGrayLogServersAsync(Settings, messageWithHeader, logType, grayLogFacility, grayLogSource, true, grayLogVersion);
});
}
var logMessage = new EonaCatLogMessage
{
@@ -203,15 +231,15 @@ namespace EonaCat.Logger.Managers
GC.SuppressFinalize(this);
}
public void Write(Exception exception, string module = null, string method = null, bool criticalException = false, bool? writeToConsole = null)
public void Write(Exception exception, string module = null, string method = null, bool criticalException = false, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1")
{
if (exception == null)
return;
Write(exception.FormatExceptionToMessage(module, method), criticalException ? ELogType.CRITICAL : ELogType.ERROR, writeToConsole);
Write(exception.FormatExceptionToMessage(module, method), criticalException ? ELogType.CRITICAL : ELogType.ERROR, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogFacility, grayLogSource, grayLogVersion);
}
public void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
public void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1")
{
if (logType == ELogType.NONE)
return;
@@ -219,7 +247,7 @@ namespace EonaCat.Logger.Managers
if (!IsRunning)
StartNewLogAsync().ConfigureAwait(false);
InternalWriteAsync(CurrentDateTme, message, logType, writeToConsole);
InternalWriteAsync(CurrentDateTme, message, logType, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogFacility, grayLogSource, grayLogVersion);
}
}
}

View File

@@ -2,10 +2,14 @@
using System.Collections.Generic;
using EonaCat.Logger.EonaCatCoreLogger;
using EonaCat.Logger.EonaCatCoreLogger.Models;
using EonaCat.Logger.GrayLog;
using EonaCat.Logger.Syslog;
namespace EonaCat.Logger.Managers
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
/// <summary>
/// Logger settings.
/// </summary>
@@ -113,6 +117,14 @@ namespace EonaCat.Logger.Managers
public List<SyslogServer> SysLogServers { get; set; }
public bool SendToSplunkServers { get; set; }
public bool SendToGrayLogServers { get; set; }
public List<SplunkServer.SplunkServer> SplunkServers { get; set; }
public List<GrayLogServer> GrayLogServers { get; set; }
/// <summary>
/// Determines if the fileLogging is enabled
/// </summary>

View File

@@ -0,0 +1,79 @@
using System;
namespace EonaCat.Logger.SplunkServer
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
/// <summary>
/// Splunk Server.
/// </summary>
public class SplunkServer
{
/// <summary>
/// SplunkHecUrl
/// </summary>
public string SplunkHecUrl
{
get
{
return _splunkHecUrl;
}
set
{
if (!string.IsNullOrWhiteSpace(_splunkHecUrl) && !_splunkHecUrl.ToLower().Contains("http"))
{
value = $"http://{value}";
}
_splunkHecUrl = value;
}
}
public bool IsHttpsHecUrl => HasHecUrl && _splunkHecUrl.ToLower().StartsWith("https");
/// <summary>
/// SplunkHecToken
/// </summary>
public string SplunkHecToken
{
get
{
return _splunkHecToken;
}
set
{
_splunkHecToken = value;
}
}
internal readonly object SendLock = new object();
private string _splunkHecUrl = "https://127.0.0.1:8088";
private string _splunkHecToken = "splunk-hec-token";
private string _hostName;
public bool HasHecToken => !string.IsNullOrWhiteSpace(SplunkHecToken) && SplunkHecToken != "splunk-hec-token";
public bool HasHecUrl => !string.IsNullOrWhiteSpace(SplunkHecUrl);
public bool IsLocalHost => HasHecUrl && (SplunkHecUrl.ToLower().Contains("127.0.0.1") || SplunkHecUrl.ToLower().Contains("localhost"));
/// <summary>
/// Instantiate the object.
/// </summary>
public SplunkServer()
{
}
/// <summary>
/// Instantiate the object.
/// </summary>
/// <param name="splunkHecUrl">splunkHecUrl.</param>
/// <param name="splunkHecToken">splunkHecToken.</param>
public SplunkServer(string splunkHecUrl, string splunkHecToken)
{
SplunkHecUrl = splunkHecUrl;
SplunkHecToken = splunkHecToken;
}
}
}

View File

@@ -5,6 +5,9 @@ using System.Text;
namespace EonaCat.Logger.Syslog
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
/// <summary>
/// Syslog server.
/// </summary>
@@ -58,7 +61,6 @@ namespace EonaCat.Logger.Syslog
}
}
internal readonly object SendLock = new object();
internal UdpClient Udp = null;
private string _Hostname = "127.0.0.1";
private int _Port = 514;