Added sysLogServer functionality

This commit is contained in:
2022-12-15 15:02:10 +01:00
parent 7783dc07e1
commit f8659ad197
17 changed files with 1008 additions and 169 deletions

View File

@@ -0,0 +1,173 @@
using System.IO;
using System;
using System.Collections.Generic;
using EonaCat.Logger.Syslog;
using System.Runtime;
namespace EonaCat.Logger.Managers
{
/// <summary>
/// Logger settings.
/// </summary>
public class LoggerSettings
{
/// <summary>
/// Header format. Provide a string that specifies how the preamble of each message should be structured. You can use variables including:
/// {ts}: UTC timestamp
/// {host}: Hostname
/// {thread}: Thread ID
/// {sev}: Severity
/// Default: {ts} {host} {thread} {sev}
/// A space will be inserted between the header and the message.
/// </summary>
public string HeaderFormat
{
get
{
return _HeaderFormat;
}
set
{
if (string.IsNullOrEmpty(value)) _HeaderFormat = "";
else _HeaderFormat = value;
}
}
/// <summary>
/// Timestamp format.
/// </summary>
public string TimestampFormat
{
get
{
return _TimestampFormat;
}
set
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(HeaderFormat));
_TimestampFormat = value;
}
}
/// <summary>
/// Enable or disable console logging.
/// Settings this to true will first validate if a console exists.
/// If a console is not available, it will be set to false.
/// </summary>
public bool EnableConsole
{
get
{
return _EnableConsole;
}
set
{
if (value) _EnableConsole = ConsoleExists();
else _EnableConsole = false;
}
}
/// <summary>
/// Minimum severity required to send a message.
/// </summary>
public ESeverity MinimumSeverity { get; set; } = ESeverity.Debug;
/// <summary>
/// Enable or disable use of color for console messages.
/// </summary>
public bool EnableColors { get; set; } = true;
/// <summary>
/// Colors to use for console messages based on message severity.
/// </summary>
public ColorSchema Colors
{
get
{
return _colors;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(Colors));
}
_colors = value;
}
}
public bool SendToSyslogServers { get; set; }
public List<SyslogServer> SysLogServers { get; set; } = new List<SyslogServer> { new SyslogServer("127.0.0.1", 514) };
/// <summary>
/// Determines if the fileLogging is enabled
/// </summary>
public bool EnableFileLogging { get; set; } = true;
private FileLoggerOptions _fileLoggerOptions;
/// <summary>
/// FileLogger settings.
/// </summary>
public FileLoggerOptions FileLoggerOptions
{
get
{
if (_fileLoggerOptions == null)
{
_fileLoggerOptions = CreateDefaultFileLoggerOptions();
}
return _fileLoggerOptions;
}
set
{
_fileLoggerOptions = value;
}
}
private FileLoggerOptions CreateDefaultFileLoggerOptions()
{
return new FileLoggerOptions();
}
/// <summary>
/// Maximum message length. Must be greater than or equal to 32. Default is 1024.
/// </summary>
public int MaxMessageLength
{
get
{
return _MaxMessageLength;
}
set
{
if (value < 32) throw new ArgumentException("Maximum message length must be at least 32.");
_MaxMessageLength = value;
}
}
private string _HeaderFormat = "{ts} {host} {thread} {sev}";
private string _TimestampFormat = "yyyy-MM-dd HH:mm:ss";
private bool _EnableConsole = true;
private int _MaxMessageLength = 1024;
private ColorSchema _colors = new ColorSchema();
private bool ConsoleExists()
{
try
{
bool test1 = Environment.UserInteractive;
bool test2 = Console.WindowHeight > 0;
return test1 && test2;
}
catch (Exception)
{
return false;
}
}
}
}