This commit is contained in:
2025-02-16 05:30:53 +01:00
parent 56be35fc15
commit 3054db7c9b
2 changed files with 24 additions and 40 deletions

View File

@@ -151,6 +151,11 @@ internal static class LogHelper
return; return;
} }
if (settings.SplunkServers == null || settings.SplunkServers.Count() == 0)
{
return;
}
var tasks = settings.SplunkServers? var tasks = settings.SplunkServers?
.Where(splunkServer => splunkServer.HasHecUrl && splunkServer.HasHecToken) .Where(splunkServer => splunkServer.HasHecUrl && splunkServer.HasHecToken)
.Select(async splunkServer => .Select(async splunkServer =>

View File

@@ -147,12 +147,6 @@ namespace EonaCat.Logger.Advanced
return _logManager.Settings.GrayLogServers.Remove(grayLogServer); return _logManager.Settings.GrayLogServers.Remove(grayLogServer);
} }
// Sets the state for sending logs to GrayLog servers.
public static void GrayLogState(bool state)
{
_logManager.Settings.SendToGrayLogServers = state;
}
// Adds a Splunk server with the specified HEC URL and token. // Adds a Splunk server with the specified HEC URL and token.
public static void AddSplunkServer(string splunkHecUrl, string splunkHecToken, bool disableSSL = false) public static void AddSplunkServer(string splunkHecUrl, string splunkHecToken, bool disableSSL = false)
{ {
@@ -170,12 +164,6 @@ namespace EonaCat.Logger.Advanced
return _logManager.Settings.SplunkServers.Remove(splunkServer); return _logManager.Settings.SplunkServers.Remove(splunkServer);
} }
// Sets the state for sending logs to Splunk servers.
public static void SplunkState(bool state)
{
_logManager.Settings.SendToSplunkServers = state;
}
// Adds a Syslog server with the specified IP address and port. // Adds a Syslog server with the specified IP address and port.
public static void AddSyslogServer(string ipAddress, int port) public static void AddSyslogServer(string ipAddress, int port)
{ {
@@ -188,56 +176,50 @@ namespace EonaCat.Logger.Advanced
return _logManager.Settings.SysLogServers.Remove(syslogServer); return _logManager.Settings.SysLogServers.Remove(syslogServer);
} }
// Sets the state for sending logs to Syslog servers.
public static void SysLogState(bool state)
{
_logManager.Settings.SendToSyslogServers = state;
}
// Logs an informational message. // Logs an informational message.
public static void Info(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Info(string message, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.INFO, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.INFO, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
// Internal method to write logs. // Internal method to write logs.
private static void WriteAsync(string message, ELogType logType, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) private static void WriteAsync(string message, ELogType logType, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
if (grayLogSettings != null) if (grayLogSettings != null)
{ {
// Log the message with specified settings. // Log the message with specified settings.
_logManager.WriteAsync(message, logType, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings.Facility, grayLogSettings.Source, grayLogSettings.Version); _logManager.WriteAsync(message, logType, writeToConsole, customSplunkSourceType, grayLogSettings.Facility, grayLogSettings.Source, grayLogSettings.Version);
} }
else else
{ {
// Log the message with default settings. // Log the message with default settings.
_logManager.WriteAsync(message, logType, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers); _logManager.WriteAsync(message, logType, writeToConsole, customSplunkSourceType);
} }
} }
// Logs a debug message. // Logs a debug message.
public static void Debug(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Debug(string message, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.DEBUG, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.DEBUG, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
// Logs an error message along with an exception. // Logs an error message along with an exception.
public static void Error(Exception exception, string message = null, bool isCriticalException = false, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Error(Exception exception, string message = null, bool isCriticalException = false, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
if (grayLogSettings != null) if (grayLogSettings != null)
{ {
// Log the exception and message with specified settings. // Log the exception and message with specified settings.
_logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, sendToSysLogServers: sendToSysLogServers, sendToSplunkServers: sendToSplunkServers, customSplunkSourceType: customSplunkSourceType, sendToGrayLogServers: sendToGrayLogServers, grayLogFacility: grayLogSettings.Facility, grayLogSource: grayLogSettings.Source, grayLogVersion: grayLogSettings.Version); _logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, customSplunkSourceType: customSplunkSourceType, grayLogFacility: grayLogSettings.Facility, grayLogSource: grayLogSettings.Source, grayLogVersion: grayLogSettings.Version);
} }
else else
{ {
// Log the exception and message with default settings. // Log the exception and message with default settings.
_logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, sendToSysLogServers: sendToSysLogServers, sendToSplunkServers: sendToSplunkServers, customSplunkSourceType: customSplunkSourceType, sendToGrayLogServers: sendToGrayLogServers); _logManager.WriteAsync(exception, message, criticalException: isCriticalException, writeToConsole: writeToConsole, customSplunkSourceType: customSplunkSourceType);
} }
} }
// Logs an error message. // Logs an error message.
public static void Error(string message = 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", bool isCriticalException = false) public static void Error(string message = null, bool? writeToConsole = null, string? customSplunkSourceType = null, string grayLogFacility = null, string grayLogSource = null, string grayLogVersion = "1.1", bool isCriticalException = false)
{ {
if (isCriticalException) if (isCriticalException)
{ {
@@ -252,27 +234,27 @@ namespace EonaCat.Logger.Advanced
} }
// Logs a warning message. // Logs a warning message.
public static void Warning(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Warning(string message, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.WARNING, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.WARNING, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
// Logs a critical message. // Logs a critical message.
public static void Critical(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Critical(string message, bool? writeToConsole = null, bool? string? customSplunkSourceType = null, bool? GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.CRITICAL, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.CRITICAL, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
// Logs a traffic message. // Logs a traffic message.
public static void Traffic(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Traffic(string message, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.TRAFFIC, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.TRAFFIC, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
// Logs a trace message. // Logs a trace message.
public static void Trace(string message, bool? writeToConsole = null, bool? sendToSysLogServers = null, bool? sendToSplunkServers = null, string? customSplunkSourceType = null, bool? sendToGrayLogServers = null, GrayLogSettings grayLogSettings = null) public static void Trace(string message, bool? writeToConsole = null, string? customSplunkSourceType = null, GrayLogSettings grayLogSettings = null)
{ {
WriteAsync(message, ELogType.TRACE, writeToConsole, sendToSysLogServers, sendToSplunkServers, customSplunkSourceType, sendToGrayLogServers, grayLogSettings); WriteAsync(message, ELogType.TRACE, writeToConsole, customSplunkSourceType, grayLogSettings);
} }
} }
} }
@@ -323,7 +305,6 @@ Custom keywords specified in LoggerSettings
var port = 9000; var port = 9000;
Logger.AddGrayLogServer(ipAddress, port); Logger.AddGrayLogServer(ipAddress, port);
Logger.GrayLogState(true);
``` ```
**Code for enabling Splunk in the above *advanced* logger class:** **Code for enabling Splunk in the above *advanced* logger class:**
@@ -333,7 +314,6 @@ var splunkHecUrl = "https://192.168.10.51:8088/services/collector";
var splunkHecToken = "cf0c7e68-e14b-48c1-949f-efdf5d388254"; var splunkHecToken = "cf0c7e68-e14b-48c1-949f-efdf5d388254";
Logger.AddSplunkServer(splunkHecUrl, splunkHecToken, true); Logger.AddSplunkServer(splunkHecUrl, splunkHecToken, true);
Logger.SplunkState(true);
``` ```
**Code for enabling SysLog in the above *advanced* logger class:** **Code for enabling SysLog in the above *advanced* logger class:**
@@ -344,7 +324,6 @@ Logger.SplunkState(true);
var port = 514; var port = 514;
Logger.AddSyslogServer(ipAddress, port); Logger.AddSyslogServer(ipAddress, port);
Logger.SysLogState(true);
``` ```