Updated
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
net7.0;
|
net7.0;
|
||||||
</TargetFrameworks>
|
</TargetFrameworks>
|
||||||
<ApplicationIcon>icon.ico</ApplicationIcon>
|
<ApplicationIcon>icon.ico</ApplicationIcon>
|
||||||
<Version>1.1.0</Version>
|
<Version>1.1.1</Version>
|
||||||
<Authors>EonaCat (Jeroen Saey)</Authors>
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Company>EonaCat (Jeroen Saey)</Company>
|
<Company>EonaCat (Jeroen Saey)</Company>
|
||||||
|
|||||||
@@ -16,70 +16,45 @@ namespace EonaCat.Logger.Managers
|
|||||||
{
|
{
|
||||||
private static readonly object FileLock = new object();
|
private static readonly object FileLock = new object();
|
||||||
|
|
||||||
/// <summary>
|
internal static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
||||||
/// Format a message with the specified header
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="settings">Logger settings</param>
|
|
||||||
/// <param name="logType">logtype for the formatted message</param>
|
|
||||||
/// <param name="currentMessage">The actual message to format with the header</param>
|
|
||||||
/// <param name="dateTime">The dateTime of the message</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string FormatMessageWithHeader(LoggerSettings settings, ELogType logType, string currentMessage, DateTime dateTime)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(currentMessage))
|
if (string.IsNullOrWhiteSpace(currentMessage))
|
||||||
{
|
|
||||||
return currentMessage;
|
return currentMessage;
|
||||||
}
|
|
||||||
|
|
||||||
if (settings == null)
|
string header = settings?.HeaderFormat ?? "[EonaCatLogger]";
|
||||||
{
|
|
||||||
return "[EonaCatLogger]" + " " + currentMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
string header = settings.HeaderFormat;
|
|
||||||
if (header.Contains("{ts}"))
|
if (header.Contains("{ts}"))
|
||||||
{
|
header = header.Replace("{ts}", dateTime.ToString(settings?.TimestampFormat));
|
||||||
header = header.Replace("{ts}", dateTime.ToString(settings.TimestampFormat));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (header.Contains("{host}"))
|
if (header.Contains("{host}"))
|
||||||
{
|
|
||||||
header = header.Replace("{host}", Dns.GetHostName());
|
header = header.Replace("{host}", Dns.GetHostName());
|
||||||
}
|
|
||||||
|
|
||||||
if (header.Contains("{thread}"))
|
if (header.Contains("{thread}"))
|
||||||
{
|
|
||||||
header = header.Replace("{thread}", Thread.CurrentThread.ManagedThreadId.ToString());
|
header = header.Replace("{thread}", Thread.CurrentThread.ManagedThreadId.ToString());
|
||||||
}
|
|
||||||
|
|
||||||
if (header.Contains("{sev}"))
|
if (header.Contains("{sev}"))
|
||||||
{
|
|
||||||
header = header.Replace("{sev}", logType.ToString());
|
header = header.Replace("{sev}", logType.ToString());
|
||||||
}
|
|
||||||
|
|
||||||
string fullMessage = AddHeaderIfNotExists(header, currentMessage);
|
string fullMessage = AddHeaderIfNotExists(settings, header, currentMessage);
|
||||||
return fullMessage;
|
return fullMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string AddHeaderIfNotExists(string header, string currentMessage)
|
private static string AddHeaderIfNotExists(LoggerSettings settings, string header, string currentMessage)
|
||||||
|
{
|
||||||
|
if (settings == null || !settings.RemoveMessagePrefix)
|
||||||
{
|
{
|
||||||
if (!currentMessage.Contains("[EonaCatLogger]"))
|
if (!currentMessage.Contains("[EonaCatLogger]"))
|
||||||
{
|
return "[EonaCatLogger] " + header + " " + currentMessage;
|
||||||
return "[EonaCatLogger]" + " " + header + " " + currentMessage;
|
}
|
||||||
}
|
|
||||||
return currentMessage;
|
return header + " " + currentMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Formats a given exception as a string
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="exception">exception</param>
|
|
||||||
/// <param name="module">The name of the module which called the code (optional)</param>
|
|
||||||
/// <param name="method">The name of the method which waws called in code (optional)</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string FormatExceptionToMessage(this Exception exception, string module = null, string method = null)
|
public static string FormatExceptionToMessage(this Exception exception, string module = null, string method = null)
|
||||||
{
|
{
|
||||||
if (exception == null) return string.Empty;
|
if (exception == null)
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
var st = new StackTrace(exception, true);
|
var st = new StackTrace(exception, true);
|
||||||
var frame = st.GetFrame(0);
|
var frame = st.GetFrame(0);
|
||||||
int fileLine = frame.GetFileLineNumber();
|
int fileLine = frame.GetFileLineNumber();
|
||||||
@@ -90,49 +65,9 @@ namespace EonaCat.Logger.Managers
|
|||||||
"--- Exception details provided by EonaCatLogger ---" + Environment.NewLine +
|
"--- Exception details provided by EonaCatLogger ---" + Environment.NewLine +
|
||||||
(!string.IsNullOrEmpty(module) ? " Module : " + module + Environment.NewLine : "") +
|
(!string.IsNullOrEmpty(module) ? " Module : " + module + Environment.NewLine : "") +
|
||||||
(!string.IsNullOrEmpty(method) ? " Method : " + method + Environment.NewLine : "") +
|
(!string.IsNullOrEmpty(method) ? " Method : " + method + Environment.NewLine : "") +
|
||||||
" Type : " + exception.GetType().ToString() + Environment.NewLine;
|
" Type : " + exception.GetType().ToString() + Environment.NewLine +
|
||||||
|
" Data : " + (exception.Data != null && exception.Data.Count > 0 ? Environment.NewLine + FormatExceptionData(exception.Data) : "(none)") + Environment.NewLine +
|
||||||
if (exception.Data != null && exception.Data.Count > 0)
|
" Inner : " + (exception.InnerException != null ? FormatInnerException(exception.InnerException) : "(null)") + Environment.NewLine +
|
||||||
{
|
|
||||||
message += " Data : " + Environment.NewLine;
|
|
||||||
foreach (DictionaryEntry curr in exception.Data)
|
|
||||||
{
|
|
||||||
message += " | " + curr.Key + ": " + curr.Value + Environment.NewLine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message += " Data : (none)" + Environment.NewLine;
|
|
||||||
}
|
|
||||||
|
|
||||||
message +=
|
|
||||||
" Inner : ";
|
|
||||||
|
|
||||||
if (exception.InnerException == null) message += "(null)" + Environment.NewLine;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message += exception.InnerException.GetType().ToString() + Environment.NewLine;
|
|
||||||
message +=
|
|
||||||
" Message : " + exception.InnerException.Message + Environment.NewLine +
|
|
||||||
" Source : " + exception.InnerException.Source + Environment.NewLine +
|
|
||||||
" StackTrace : " + exception.InnerException.StackTrace + Environment.NewLine +
|
|
||||||
" ToString : " + exception.InnerException.ToString() + Environment.NewLine;
|
|
||||||
|
|
||||||
if (exception.InnerException.Data != null && exception.InnerException.Data.Count > 0)
|
|
||||||
{
|
|
||||||
message += " Data : " + Environment.NewLine;
|
|
||||||
foreach (DictionaryEntry curr in exception.Data)
|
|
||||||
{
|
|
||||||
message += " | " + curr.Key + ": " + curr.Value + Environment.NewLine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message += " Data : (none)" + Environment.NewLine;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
message +=
|
|
||||||
" Message : " + exception.Message + Environment.NewLine +
|
" Message : " + exception.Message + Environment.NewLine +
|
||||||
" Source : " + exception.Source + Environment.NewLine +
|
" Source : " + exception.Source + Environment.NewLine +
|
||||||
" StackTrace : " + exception.StackTrace + Environment.NewLine +
|
" StackTrace : " + exception.StackTrace + Environment.NewLine +
|
||||||
@@ -140,14 +75,45 @@ namespace EonaCat.Logger.Managers
|
|||||||
" File : " + filename + Environment.NewLine +
|
" File : " + filename + Environment.NewLine +
|
||||||
" ToString : " + exception.ToString() + Environment.NewLine +
|
" ToString : " + exception.ToString() + Environment.NewLine +
|
||||||
"---";
|
"---";
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string FormatExceptionData(IDictionary data)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (DictionaryEntry entry in data)
|
||||||
|
{
|
||||||
|
sb.Append(" | ")
|
||||||
|
.Append(entry.Key)
|
||||||
|
.Append(": ")
|
||||||
|
.Append(entry.Value)
|
||||||
|
.Append(Environment.NewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FormatInnerException(Exception innerException)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine(innerException.GetType().ToString())
|
||||||
|
.AppendLine(" Message : " + innerException.Message)
|
||||||
|
.AppendLine(" Source : " + innerException.Source)
|
||||||
|
.AppendLine(" StackTrace : " + innerException.StackTrace)
|
||||||
|
.AppendLine(" ToString : " + innerException.ToString())
|
||||||
|
.Append(" Data : ")
|
||||||
|
.AppendLine(innerException.Data != null && innerException.Data.Count > 0 ? Environment.NewLine + FormatExceptionData(innerException.Data) : "(none)");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
internal static void SendConsole(LoggerSettings settings, ELogType logType, string message, bool writeToConsole)
|
internal static void SendConsole(LoggerSettings settings, ELogType logType, string message, bool writeToConsole)
|
||||||
{
|
{
|
||||||
if (settings == null) return;
|
if (settings == null || !writeToConsole || string.IsNullOrWhiteSpace(message))
|
||||||
if (!writeToConsole) return;
|
return;
|
||||||
if (string.IsNullOrWhiteSpace(message)) return;
|
|
||||||
|
|
||||||
if (settings.EnableColors)
|
if (settings.EnableColors)
|
||||||
{
|
{
|
||||||
@@ -203,10 +169,8 @@ namespace EonaCat.Logger.Managers
|
|||||||
{
|
{
|
||||||
lock (FileLock)
|
lock (FileLock)
|
||||||
{
|
{
|
||||||
if (logger == null) return;
|
if (logger == null || settings == null || !settings.EnableFileLogging || string.IsNullOrWhiteSpace(message))
|
||||||
if (settings == null) return;
|
return;
|
||||||
if (!settings.EnableFileLogging) return;
|
|
||||||
if (string.IsNullOrWhiteSpace(message)) return;
|
|
||||||
|
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
bool completed = false;
|
bool completed = false;
|
||||||
@@ -214,33 +178,29 @@ namespace EonaCat.Logger.Managers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (logType == ELogType.CRITICAL)
|
switch (logType)
|
||||||
{
|
{
|
||||||
|
case ELogType.CRITICAL:
|
||||||
logger.LogCritical(message);
|
logger.LogCritical(message);
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.DEBUG)
|
case ELogType.DEBUG:
|
||||||
{
|
|
||||||
logger.LogDebug(message);
|
logger.LogDebug(message);
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.ERROR)
|
case ELogType.ERROR:
|
||||||
{
|
|
||||||
logger.LogError(message);
|
logger.LogError(message);
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.INFO)
|
case ELogType.INFO:
|
||||||
{
|
|
||||||
logger.LogInformation(message);
|
logger.LogInformation(message);
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.TRACE)
|
case ELogType.TRACE:
|
||||||
{
|
|
||||||
logger.LogTrace(message);
|
logger.LogTrace(message);
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.TRAFFIC)
|
case ELogType.TRAFFIC:
|
||||||
{
|
|
||||||
logger.LogTrace($"[TRAFFIC] {message}");
|
logger.LogTrace($"[TRAFFIC] {message}");
|
||||||
}
|
break;
|
||||||
else if (logType == ELogType.WARNING)
|
case ELogType.WARNING:
|
||||||
{
|
|
||||||
logger.LogWarning(message);
|
logger.LogWarning(message);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
completed = true;
|
completed = true;
|
||||||
}
|
}
|
||||||
@@ -257,9 +217,8 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
internal static void SendToSysLogServers(LoggerSettings settings, string message)
|
internal static void SendToSysLogServers(LoggerSettings settings, string message)
|
||||||
{
|
{
|
||||||
if (settings == null) return;
|
if (settings == null || !settings.SendToSyslogServers || string.IsNullOrWhiteSpace(message))
|
||||||
if (!settings.SendToSyslogServers) return;
|
return;
|
||||||
if (string.IsNullOrWhiteSpace(message)) return;
|
|
||||||
|
|
||||||
if (settings.SysLogServers == null || !settings.SysLogServers.Any())
|
if (settings.SysLogServers == null || !settings.SysLogServers.Any())
|
||||||
{
|
{
|
||||||
@@ -294,4 +253,5 @@ namespace EonaCat.Logger.Managers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using EonaCat.Logger.Exceptions;
|
using EonaCat.Logger.Exceptions;
|
||||||
using EonaCat.Logger.Helpers;
|
|
||||||
using EonaCat.Logger.Syslog;
|
using EonaCat.Logger.Syslog;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -20,66 +19,42 @@ namespace EonaCat.Logger.Managers
|
|||||||
public partial class LogManager : ILogManager, IDisposable
|
public partial class LogManager : ILogManager, IDisposable
|
||||||
{
|
{
|
||||||
private DateTime _logDate;
|
private DateTime _logDate;
|
||||||
public ELogType LogType;
|
public ILoggerProvider LoggerProvider { get; private set; }
|
||||||
private ILoggerProvider LoggerProvider { get; set; }
|
public ILoggerFactory LoggerFactory { get; private set; }
|
||||||
private ILoggerFactory LoggerFactory { get; set; }
|
public ILogger Logger { get; private set; }
|
||||||
private ILogger Logger { get; set; }
|
public string CurrentLogFile => LoggerProvider is FileLoggerProvider fileLoggerProvider ? fileLoggerProvider.LogFile : string.Empty;
|
||||||
public string CurrentLogFile => LoggerProvider != null ? ((FileLoggerProvider)LoggerProvider).LogFile : string.Empty;
|
|
||||||
|
|
||||||
public bool IsRunning { get; private set; }
|
public bool IsRunning { get; private set; }
|
||||||
|
|
||||||
public StreamWriter Output { get; private set; }
|
|
||||||
|
|
||||||
public string CategoryName { get; set; }
|
public string CategoryName { get; set; }
|
||||||
|
public string Id { get; }
|
||||||
public readonly string Id;
|
|
||||||
|
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private static LogManager _instance;
|
private static LogManager _instance;
|
||||||
private LoggerSettings _settings;
|
private LoggerSettings _settings;
|
||||||
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
||||||
private CancellationToken _token;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Default Logger Instance with it's default configuration
|
|
||||||
/// </summary>
|
|
||||||
public static LogManager Instance => InstanceInit();
|
public static LogManager Instance => InstanceInit();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Logging settings.
|
|
||||||
/// </summary>
|
|
||||||
public LoggerSettings Settings
|
public LoggerSettings Settings
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_settings == null)
|
_settings ??= new LoggerSettings();
|
||||||
{
|
|
||||||
_settings = new LoggerSettings();
|
|
||||||
}
|
|
||||||
return _settings;
|
return _settings;
|
||||||
}
|
}
|
||||||
|
set => _settings = value;
|
||||||
set
|
|
||||||
{
|
|
||||||
_settings = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LogManager InstanceInit()
|
private static LogManager InstanceInit()
|
||||||
{
|
{
|
||||||
if (_instance == null)
|
_instance ??= new LogManager(null, id: "EonaCat");
|
||||||
{
|
|
||||||
_instance = new LogManager(null, id: "EonaCat");
|
|
||||||
}
|
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (_disposed)
|
if (_disposed)
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
@@ -93,43 +68,38 @@ namespace EonaCat.Logger.Managers
|
|||||||
private void StartNewLog()
|
private void StartNewLog()
|
||||||
{
|
{
|
||||||
if (_tokenSource.IsCancellationRequested)
|
if (_tokenSource.IsCancellationRequested)
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
DateTime now = DateTime.Now;
|
DateTime now = DateTime.UtcNow;
|
||||||
|
|
||||||
if (IsRunning && now.Date > _logDate.Date)
|
if (IsRunning && now.Date > _logDate.Date)
|
||||||
{
|
|
||||||
StopLogging();
|
StopLogging();
|
||||||
}
|
|
||||||
IsRunning = true;
|
IsRunning = true;
|
||||||
|
|
||||||
IServiceCollection serviceCollection = new ServiceCollection();
|
IServiceCollection serviceCollection = new ServiceCollection();
|
||||||
serviceCollection.AddLogging(builder => builder.AddEonaCatFileLogger(configuration =>
|
serviceCollection.AddLogging(builder => builder.AddEonaCatFileLogger(configuration =>
|
||||||
{
|
{
|
||||||
configuration.MaxWriteTries = Settings.FileLoggerOptions.MaxWriteTries;
|
var fileLoggerOptions = Settings.FileLoggerOptions;
|
||||||
configuration.RetainedFileCountLimit = Settings.FileLoggerOptions.RetainedFileCountLimit;
|
configuration.MaxWriteTries = fileLoggerOptions.MaxWriteTries;
|
||||||
configuration.FlushPeriod = Settings.FileLoggerOptions.FlushPeriod;
|
configuration.RetainedFileCountLimit = fileLoggerOptions.RetainedFileCountLimit;
|
||||||
configuration.IsEnabled = Settings.FileLoggerOptions.IsEnabled;
|
configuration.FlushPeriod = fileLoggerOptions.FlushPeriod;
|
||||||
configuration.BatchSize = Settings.FileLoggerOptions.BatchSize;
|
configuration.IsEnabled = fileLoggerOptions.IsEnabled;
|
||||||
configuration.FileSizeLimit = Settings.FileLoggerOptions.FileSizeLimit;
|
configuration.BatchSize = fileLoggerOptions.BatchSize;
|
||||||
configuration.LogDirectory = Settings.FileLoggerOptions.LogDirectory;
|
configuration.FileSizeLimit = fileLoggerOptions.FileSizeLimit;
|
||||||
configuration.FileNamePrefix = Settings.FileLoggerOptions.FileNamePrefix;
|
configuration.LogDirectory = fileLoggerOptions.LogDirectory;
|
||||||
configuration.MaxRolloverFiles = Settings.FileLoggerOptions.MaxRolloverFiles;
|
configuration.FileNamePrefix = fileLoggerOptions.FileNamePrefix;
|
||||||
|
configuration.MaxRolloverFiles = fileLoggerOptions.MaxRolloverFiles;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||||
LoggerProvider = serviceProvider.GetService<ILoggerProvider>();
|
LoggerProvider = serviceProvider.GetService<ILoggerProvider>();
|
||||||
LoggerFactory = serviceProvider.GetService<ILoggerFactory>();
|
LoggerFactory = serviceProvider.GetService<ILoggerFactory>();
|
||||||
|
|
||||||
CategoryName = CategoryName ?? string.Empty;
|
CategoryName ??= string.Empty;
|
||||||
Logger = LoggerFactory.CreateLogger(CategoryName);
|
Logger = LoggerFactory.CreateLogger(CategoryName);
|
||||||
|
|
||||||
if (!Directory.Exists(Settings.FileLoggerOptions.LogDirectory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(Settings.FileLoggerOptions.LogDirectory);
|
Directory.CreateDirectory(Settings.FileLoggerOptions.LogDirectory);
|
||||||
}
|
|
||||||
|
|
||||||
_logDate = now;
|
_logDate = now;
|
||||||
|
|
||||||
@@ -139,60 +109,35 @@ namespace EonaCat.Logger.Managers
|
|||||||
public void Assert(bool condition, string message)
|
public void Assert(bool condition, string message)
|
||||||
{
|
{
|
||||||
if (!condition)
|
if (!condition)
|
||||||
{
|
|
||||||
throw new EonaCatLoggerAssertionException(message);
|
throw new EonaCatLoggerAssertionException(message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void Write(DateTime dateTime, string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
private void Write(DateTime dateTime, string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(message)) return;
|
if (string.IsNullOrEmpty(message) || logType < ELogType.INFO)
|
||||||
if (logType < ELogType.INFO) return;
|
return;
|
||||||
|
|
||||||
string remainder = "";
|
string currentMessage = message.Length > _settings.MaxMessageLength ? message.Substring(0, _settings.MaxMessageLength) : message;
|
||||||
string currentMessage;
|
string remainder = message.Length > _settings.MaxMessageLength ? message.Substring(_settings.MaxMessageLength) : "";
|
||||||
|
|
||||||
if (message.Length > _settings.MaxMessageLength)
|
|
||||||
{
|
|
||||||
currentMessage = message.Substring(0, _settings.MaxMessageLength);
|
|
||||||
remainder = message.Substring(_settings.MaxMessageLength, (message.Length - _settings.MaxMessageLength));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
currentMessage = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fullMessage = LogHelper.FormatMessageWithHeader(_settings, logType, currentMessage, dateTime);
|
var fullMessage = LogHelper.FormatMessageWithHeader(_settings, logType, currentMessage, dateTime);
|
||||||
|
|
||||||
if (writeToConsole == null)
|
writeToConsole ??= _settings.EnableConsole;
|
||||||
{
|
LogHelper.SendConsole(_settings, logType, fullMessage, writeToConsole.Value);
|
||||||
writeToConsole = _settings.EnableConsole;
|
|
||||||
}
|
|
||||||
|
|
||||||
LogHelper.SendConsole(_settings, logType, fullMessage, writeToConsole == true);
|
|
||||||
|
|
||||||
LogHelper.SendFile(Logger, _settings, logType, fullMessage);
|
LogHelper.SendFile(Logger, _settings, logType, fullMessage);
|
||||||
|
|
||||||
LogHelper.SendToSysLogServers(_settings, fullMessage);
|
LogHelper.SendToSysLogServers(_settings, fullMessage);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(remainder))
|
if (!string.IsNullOrEmpty(remainder))
|
||||||
{
|
|
||||||
Write(dateTime, remainder, logType, writeToConsole);
|
Write(dateTime, remainder, logType, writeToConsole);
|
||||||
}
|
|
||||||
|
|
||||||
var logMessage = new EonaCatLogMessage
|
var logMessage = new EonaCatLogMessage
|
||||||
{
|
{
|
||||||
DateTime = dateTime,
|
DateTime = dateTime,
|
||||||
Message = currentMessage,
|
Message = currentMessage,
|
||||||
LogType = logType
|
LogType = logType,
|
||||||
|
Origin = string.IsNullOrWhiteSpace(Settings?.LogOrigin) ? "LogManager" : Settings.LogOrigin
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Settings != null)
|
|
||||||
{
|
|
||||||
logMessage.Origin = string.IsNullOrWhiteSpace(Settings.LogOrigin) ? "LogManager" : Settings.LogOrigin;
|
|
||||||
Settings?.OnLogEvent(logMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
Settings?.OnLogEvent(logMessage);
|
Settings?.OnLogEvent(logMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,8 +145,11 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
public LogManager(LoggerSettings settings, string serverIp, int serverPort, string id = "EonaCatLogger")
|
public LogManager(LoggerSettings settings, string serverIp, int serverPort, string id = "EonaCatLogger")
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(serverIp)) throw new ArgumentNullException(nameof(serverIp));
|
if (string.IsNullOrEmpty(serverIp))
|
||||||
if (serverPort < 0) throw new ArgumentException("Server port must be zero or greater.");
|
throw new ArgumentNullException(nameof(serverIp));
|
||||||
|
|
||||||
|
if (serverPort < 0)
|
||||||
|
throw new ArgumentException("Server port must be zero or greater.");
|
||||||
|
|
||||||
settings.SysLogServers = new List<SyslogServer>
|
settings.SysLogServers = new List<SyslogServer>
|
||||||
{
|
{
|
||||||
@@ -224,43 +172,26 @@ namespace EonaCat.Logger.Managers
|
|||||||
private void SetupFileLogger(LoggerSettings settings = null, string logFolder = null, bool defaultPrefix = true)
|
private void SetupFileLogger(LoggerSettings settings = null, string logFolder = null, bool defaultPrefix = true)
|
||||||
{
|
{
|
||||||
if (settings == null)
|
if (settings == null)
|
||||||
{
|
|
||||||
// Create default loggingSettings
|
|
||||||
Settings = settings;
|
|
||||||
settings = Settings;
|
settings = Settings;
|
||||||
}
|
|
||||||
|
|
||||||
if (!settings.EnableFileLogging) return;
|
if (!settings.EnableFileLogging)
|
||||||
|
return;
|
||||||
|
|
||||||
if (logFolder != null)
|
if (logFolder != null)
|
||||||
{
|
settings.FileLoggerOptions.LogDirectory = logFolder;
|
||||||
Settings.FileLoggerOptions.LogDirectory = logFolder;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(Settings.FileLoggerOptions.FileNamePrefix))
|
if (string.IsNullOrWhiteSpace(settings.FileLoggerOptions.FileNamePrefix))
|
||||||
{
|
settings.FileLoggerOptions.FileNamePrefix = defaultPrefix ? "EonaCat" : string.Empty;
|
||||||
if (defaultPrefix)
|
|
||||||
{
|
|
||||||
Settings.FileLoggerOptions.FileNamePrefix = "EonaCat";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Settings.FileLoggerOptions.FileNamePrefix = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetupLogManager()
|
private void SetupLogManager()
|
||||||
{
|
{
|
||||||
_token = _tokenSource.Token;
|
|
||||||
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
|
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
|
||||||
|
Console.CancelKeyPress += Console_CancelKeyPress;
|
||||||
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
|
_logDate = DateTime.UtcNow;
|
||||||
|
|
||||||
_logDate = DateTime.Now;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||||
{
|
{
|
||||||
Dispose(true);
|
Dispose(true);
|
||||||
}
|
}
|
||||||
@@ -273,8 +204,8 @@ namespace EonaCat.Logger.Managers
|
|||||||
private void StopLogging()
|
private void StopLogging()
|
||||||
{
|
{
|
||||||
IsRunning = false;
|
IsRunning = false;
|
||||||
Write(DateTime.Now, $"{DllInfo.ApplicationName} stopped.");
|
Write(DateTime.UtcNow, $"{DllInfo.ApplicationName} stopped.");
|
||||||
Task.Delay(500, _token);
|
Task.Delay(500, _tokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LogManager(string logFolder = null, bool defaultPrefix = true)
|
public LogManager(string logFolder = null, bool defaultPrefix = true)
|
||||||
@@ -285,23 +216,21 @@ namespace EonaCat.Logger.Managers
|
|||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (exception == null) return;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
public void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
||||||
{
|
{
|
||||||
if (logType == ELogType.NONE)
|
if (logType == ELogType.NONE)
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
DateTime now = DateTime.Now;
|
DateTime now = DateTime.UtcNow;
|
||||||
|
|
||||||
if (!IsRunning)
|
if (!IsRunning)
|
||||||
{
|
|
||||||
StartNewLog();
|
StartNewLog();
|
||||||
}
|
|
||||||
|
|
||||||
Write(now, message, logType, writeToConsole);
|
Write(now, message, logType, writeToConsole);
|
||||||
}
|
}
|
||||||
@@ -309,14 +238,14 @@ namespace EonaCat.Logger.Managers
|
|||||||
public void DeleteCurrentLogFile()
|
public void DeleteCurrentLogFile()
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(CurrentLogFile))
|
if (!string.IsNullOrWhiteSpace(CurrentLogFile))
|
||||||
{
|
|
||||||
File.Delete(CurrentLogFile);
|
File.Delete(CurrentLogFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDisposable.Dispose()
|
|
||||||
{
|
|
||||||
Dispose(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,11 @@ namespace EonaCat.Logger.Managers
|
|||||||
public event LogDelegate OnLog;
|
public event LogDelegate OnLog;
|
||||||
public delegate void LogDelegate(EonaCatLogMessage message);
|
public delegate void LogDelegate(EonaCatLogMessage message);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if we need to remove the prefix [EonaCatLogger] from each message (default: false)
|
||||||
|
/// </summary>
|
||||||
|
public bool RemoveMessagePrefix { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Header format. Provide a string that specifies how the preamble of each message should be structured. You can use variables including:
|
/// Header format. Provide a string that specifies how the preamble of each message should be structured. You can use variables including:
|
||||||
@@ -148,7 +153,7 @@ namespace EonaCat.Logger.Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set the origin of where the OnLog event was inititated
|
/// Set the origin of where the OnLog event was initiated
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string LogOrigin { get; set; }
|
public string LogOrigin { get; set; }
|
||||||
|
|
||||||
@@ -159,7 +164,7 @@ namespace EonaCat.Logger.Managers
|
|||||||
private ColorSchema _colors = new ColorSchema();
|
private ColorSchema _colors = new ColorSchema();
|
||||||
|
|
||||||
|
|
||||||
private bool ConsoleExists()
|
private static bool ConsoleExists()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user