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

@@ -147,12 +147,6 @@ namespace EonaCat.Logger.Advanced
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.
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);
}
// 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.
public static void AddSyslogServer(string ipAddress, int port)
{
@@ -188,56 +176,50 @@ namespace EonaCat.Logger.Advanced
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.
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.
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)
{
// 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
{
// 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.
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.
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)
{
// 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
{
// 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.
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)
{
@@ -252,27 +234,27 @@ namespace EonaCat.Logger.Advanced
}
// 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.
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.
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.
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;
Logger.AddGrayLogServer(ipAddress, port);
Logger.GrayLogState(true);
```
**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";
Logger.AddSplunkServer(splunkHecUrl, splunkHecToken, true);
Logger.SplunkState(true);
```
**Code for enabling SysLog in the above *advanced* logger class:**
@@ -344,7 +324,6 @@ Logger.SplunkState(true);
var port = 514;
Logger.AddSyslogServer(ipAddress, port);
Logger.SysLogState(true);
```