Updated
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using EonaCat.Logger.Exceptions;
|
||||
using EonaCat.Logger.Helpers;
|
||||
using EonaCat.Logger.Syslog;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -20,66 +19,42 @@ namespace EonaCat.Logger.Managers
|
||||
public partial class LogManager : ILogManager, IDisposable
|
||||
{
|
||||
private DateTime _logDate;
|
||||
public ELogType LogType;
|
||||
private ILoggerProvider LoggerProvider { get; set; }
|
||||
private ILoggerFactory LoggerFactory { get; set; }
|
||||
private ILogger Logger { get; set; }
|
||||
public string CurrentLogFile => LoggerProvider != null ? ((FileLoggerProvider)LoggerProvider).LogFile : string.Empty;
|
||||
public ILoggerProvider LoggerProvider { get; private set; }
|
||||
public ILoggerFactory LoggerFactory { get; private set; }
|
||||
public ILogger Logger { get; private set; }
|
||||
public string CurrentLogFile => LoggerProvider is FileLoggerProvider fileLoggerProvider ? fileLoggerProvider.LogFile : string.Empty;
|
||||
|
||||
public bool IsRunning { get; private set; }
|
||||
|
||||
public StreamWriter Output { get; private set; }
|
||||
|
||||
public string CategoryName { get; set; }
|
||||
|
||||
public readonly string Id;
|
||||
public string Id { get; }
|
||||
|
||||
private bool _disposed;
|
||||
private static LogManager _instance;
|
||||
private LoggerSettings _settings;
|
||||
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
||||
private CancellationToken _token;
|
||||
|
||||
/// <summary>
|
||||
/// Default Logger Instance with it's default configuration
|
||||
/// </summary>
|
||||
public static LogManager Instance => InstanceInit();
|
||||
|
||||
/// <summary>
|
||||
/// Logging settings.
|
||||
/// </summary>
|
||||
public LoggerSettings Settings
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_settings == null)
|
||||
{
|
||||
_settings = new LoggerSettings();
|
||||
}
|
||||
_settings ??= new LoggerSettings();
|
||||
return _settings;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_settings = value;
|
||||
}
|
||||
set => _settings = value;
|
||||
}
|
||||
|
||||
private static LogManager InstanceInit()
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new LogManager(null, id: "EonaCat");
|
||||
}
|
||||
_instance ??= new LogManager(null, id: "EonaCat");
|
||||
return _instance;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
@@ -93,43 +68,38 @@ namespace EonaCat.Logger.Managers
|
||||
private void StartNewLog()
|
||||
{
|
||||
if (_tokenSource.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime now = DateTime.UtcNow;
|
||||
|
||||
if (IsRunning && now.Date > _logDate.Date)
|
||||
{
|
||||
StopLogging();
|
||||
}
|
||||
|
||||
IsRunning = true;
|
||||
|
||||
IServiceCollection serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddLogging(builder => builder.AddEonaCatFileLogger(configuration =>
|
||||
{
|
||||
configuration.MaxWriteTries = Settings.FileLoggerOptions.MaxWriteTries;
|
||||
configuration.RetainedFileCountLimit = Settings.FileLoggerOptions.RetainedFileCountLimit;
|
||||
configuration.FlushPeriod = Settings.FileLoggerOptions.FlushPeriod;
|
||||
configuration.IsEnabled = Settings.FileLoggerOptions.IsEnabled;
|
||||
configuration.BatchSize = Settings.FileLoggerOptions.BatchSize;
|
||||
configuration.FileSizeLimit = Settings.FileLoggerOptions.FileSizeLimit;
|
||||
configuration.LogDirectory = Settings.FileLoggerOptions.LogDirectory;
|
||||
configuration.FileNamePrefix = Settings.FileLoggerOptions.FileNamePrefix;
|
||||
configuration.MaxRolloverFiles = Settings.FileLoggerOptions.MaxRolloverFiles;
|
||||
var fileLoggerOptions = Settings.FileLoggerOptions;
|
||||
configuration.MaxWriteTries = fileLoggerOptions.MaxWriteTries;
|
||||
configuration.RetainedFileCountLimit = fileLoggerOptions.RetainedFileCountLimit;
|
||||
configuration.FlushPeriod = fileLoggerOptions.FlushPeriod;
|
||||
configuration.IsEnabled = fileLoggerOptions.IsEnabled;
|
||||
configuration.BatchSize = fileLoggerOptions.BatchSize;
|
||||
configuration.FileSizeLimit = fileLoggerOptions.FileSizeLimit;
|
||||
configuration.LogDirectory = fileLoggerOptions.LogDirectory;
|
||||
configuration.FileNamePrefix = fileLoggerOptions.FileNamePrefix;
|
||||
configuration.MaxRolloverFiles = fileLoggerOptions.MaxRolloverFiles;
|
||||
}));
|
||||
|
||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
LoggerProvider = serviceProvider.GetService<ILoggerProvider>();
|
||||
LoggerFactory = serviceProvider.GetService<ILoggerFactory>();
|
||||
|
||||
CategoryName = CategoryName ?? string.Empty;
|
||||
CategoryName ??= string.Empty;
|
||||
Logger = LoggerFactory.CreateLogger(CategoryName);
|
||||
|
||||
if (!Directory.Exists(Settings.FileLoggerOptions.LogDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(Settings.FileLoggerOptions.LogDirectory);
|
||||
}
|
||||
Directory.CreateDirectory(Settings.FileLoggerOptions.LogDirectory);
|
||||
|
||||
_logDate = now;
|
||||
|
||||
@@ -139,60 +109,35 @@ namespace EonaCat.Logger.Managers
|
||||
public void Assert(bool condition, string message)
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
throw new EonaCatLoggerAssertionException(message);
|
||||
}
|
||||
}
|
||||
|
||||
private void Write(DateTime dateTime, string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message)) return;
|
||||
if (logType < ELogType.INFO) return;
|
||||
if (string.IsNullOrEmpty(message) || logType < ELogType.INFO)
|
||||
return;
|
||||
|
||||
string remainder = "";
|
||||
string currentMessage;
|
||||
|
||||
if (message.Length > _settings.MaxMessageLength)
|
||||
{
|
||||
currentMessage = message.Substring(0, _settings.MaxMessageLength);
|
||||
remainder = message.Substring(_settings.MaxMessageLength, (message.Length - _settings.MaxMessageLength));
|
||||
}
|
||||
else
|
||||
{
|
||||
currentMessage = message;
|
||||
}
|
||||
string currentMessage = message.Length > _settings.MaxMessageLength ? message.Substring(0, _settings.MaxMessageLength) : message;
|
||||
string remainder = message.Length > _settings.MaxMessageLength ? message.Substring(_settings.MaxMessageLength) : "";
|
||||
|
||||
var fullMessage = LogHelper.FormatMessageWithHeader(_settings, logType, currentMessage, dateTime);
|
||||
|
||||
if (writeToConsole == null)
|
||||
{
|
||||
writeToConsole = _settings.EnableConsole;
|
||||
}
|
||||
|
||||
LogHelper.SendConsole(_settings, logType, fullMessage, writeToConsole == true);
|
||||
|
||||
writeToConsole ??= _settings.EnableConsole;
|
||||
LogHelper.SendConsole(_settings, logType, fullMessage, writeToConsole.Value);
|
||||
LogHelper.SendFile(Logger, _settings, logType, fullMessage);
|
||||
|
||||
LogHelper.SendToSysLogServers(_settings, fullMessage);
|
||||
|
||||
if (!string.IsNullOrEmpty(remainder))
|
||||
{
|
||||
Write(dateTime, remainder, logType, writeToConsole);
|
||||
}
|
||||
|
||||
var logMessage = new EonaCatLogMessage
|
||||
{
|
||||
DateTime = dateTime,
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -200,13 +145,16 @@ namespace EonaCat.Logger.Managers
|
||||
|
||||
public LogManager(LoggerSettings settings, string serverIp, int serverPort, string id = "EonaCatLogger")
|
||||
{
|
||||
if (string.IsNullOrEmpty(serverIp)) throw new ArgumentNullException(nameof(serverIp));
|
||||
if (serverPort < 0) throw new ArgumentException("Server port must be zero or greater.");
|
||||
if (string.IsNullOrEmpty(serverIp))
|
||||
throw new ArgumentNullException(nameof(serverIp));
|
||||
|
||||
if (serverPort < 0)
|
||||
throw new ArgumentException("Server port must be zero or greater.");
|
||||
|
||||
settings.SysLogServers = new List<SyslogServer>
|
||||
{
|
||||
new SyslogServer(serverIp, serverPort)
|
||||
};
|
||||
{
|
||||
new SyslogServer(serverIp, serverPort)
|
||||
};
|
||||
|
||||
Id = id;
|
||||
Settings = settings;
|
||||
@@ -224,43 +172,26 @@ namespace EonaCat.Logger.Managers
|
||||
private void SetupFileLogger(LoggerSettings settings = null, string logFolder = null, bool defaultPrefix = true)
|
||||
{
|
||||
if (settings == null)
|
||||
{
|
||||
// Create default loggingSettings
|
||||
Settings = settings;
|
||||
settings = Settings;
|
||||
}
|
||||
|
||||
if (!settings.EnableFileLogging) return;
|
||||
if (!settings.EnableFileLogging)
|
||||
return;
|
||||
|
||||
if (logFolder != null)
|
||||
{
|
||||
Settings.FileLoggerOptions.LogDirectory = logFolder;
|
||||
}
|
||||
settings.FileLoggerOptions.LogDirectory = logFolder;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Settings.FileLoggerOptions.FileNamePrefix))
|
||||
{
|
||||
if (defaultPrefix)
|
||||
{
|
||||
Settings.FileLoggerOptions.FileNamePrefix = "EonaCat";
|
||||
}
|
||||
else
|
||||
{
|
||||
Settings.FileLoggerOptions.FileNamePrefix = string.Empty;
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(settings.FileLoggerOptions.FileNamePrefix))
|
||||
settings.FileLoggerOptions.FileNamePrefix = defaultPrefix ? "EonaCat" : string.Empty;
|
||||
}
|
||||
|
||||
private void SetupLogManager()
|
||||
{
|
||||
_token = _tokenSource.Token;
|
||||
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
|
||||
|
||||
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
|
||||
|
||||
_logDate = DateTime.Now;
|
||||
Console.CancelKeyPress += Console_CancelKeyPress;
|
||||
_logDate = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
@@ -273,8 +204,8 @@ namespace EonaCat.Logger.Managers
|
||||
private void StopLogging()
|
||||
{
|
||||
IsRunning = false;
|
||||
Write(DateTime.Now, $"{DllInfo.ApplicationName} stopped.");
|
||||
Task.Delay(500, _token);
|
||||
Write(DateTime.UtcNow, $"{DllInfo.ApplicationName} stopped.");
|
||||
Task.Delay(500, _tokenSource.Token);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (exception == null) return;
|
||||
if (exception == null)
|
||||
return;
|
||||
|
||||
Write(exception.FormatExceptionToMessage(module, method), criticalException ? ELogType.CRITICAL : ELogType.ERROR, writeToConsole);
|
||||
}
|
||||
|
||||
public void Write(string message, ELogType logType = ELogType.INFO, bool? writeToConsole = null)
|
||||
{
|
||||
if (logType == ELogType.NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime now = DateTime.UtcNow;
|
||||
|
||||
if (!IsRunning)
|
||||
{
|
||||
StartNewLog();
|
||||
}
|
||||
|
||||
Write(now, message, logType, writeToConsole);
|
||||
}
|
||||
@@ -309,14 +238,14 @@ namespace EonaCat.Logger.Managers
|
||||
public void DeleteCurrentLogFile()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(CurrentLogFile))
|
||||
{
|
||||
File.Delete(CurrentLogFile);
|
||||
}
|
||||
}
|
||||
|
||||
void IDisposable.Dispose()
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user