From 795f9807d5ea5353b9361d677dd509cdbfe09f52 Mon Sep 17 00:00:00 2001 From: EonaCat Date: Fri, 16 Dec 2022 12:59:34 +0100 Subject: [PATCH] Updated --- EonaCat.Logger/EonaCat.Logger.csproj | 38 +++++++-------- .../Extensions/FileLoggerFactoryExtensions.cs | 4 +- .../EonaCatCoreLogger/FileLoggerProvider.cs | 3 +- .../Internal/BatchingLogger.cs | 16 ++++++- .../Internal/BatchingLoggerProvider.cs | 7 ++- .../Models/EonaCatLogMessage.cs | 1 + EonaCat.Logger/Managers/LogManager.cs | 47 +++++++++++++------ EonaCat.Logger/Managers/LoggerSettings.cs | 28 +++++++---- 8 files changed, 96 insertions(+), 48 deletions(-) diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj index b6c1105..48b645b 100644 --- a/EonaCat.Logger/EonaCat.Logger.csproj +++ b/EonaCat.Logger/EonaCat.Logger.csproj @@ -8,7 +8,7 @@ net6.0; icon.ico - 1.0.2 + 1.0.3 EonaCat (Jeroen Saey) true EonaCat (Jeroen Saey) @@ -17,7 +17,7 @@ EonaCat.Logger is a logging library created for .NET Standard. Public release version EonaCat (Jeroen Saey) - EonaCat, Logger, .NET Standard, EonaCatLogger, Log, Writer Jeroen, Saey + EonaCat;Logger;.NET Standard;EonaCatLogger;Log;Writer;Jeroen;Saey README.md True @@ -27,21 +27,21 @@ git - - - - True - \ - - - True - \ - - - True - - - + + + + True + \ + + + True + \ + + + True + + + @@ -54,10 +54,6 @@ True \ - - True - \ - True \ diff --git a/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs b/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs index 8a9d1ba..428f75c 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs @@ -1,7 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using EonaCat.Logger.Helpers; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; +using System.Runtime; namespace EonaCat.Logger.Extensions { diff --git a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs index 79f2fa6..f93889a 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs @@ -126,9 +126,8 @@ namespace EonaCat.Logger System.IO.StreamWriter file = new System.IO.StreamWriter(LogFile, true); foreach (LogMessage item in group) { - file.WriteAsync(item.Message); + file.Write(item.Message); } - file.Close(); completed = true; } diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs index c3dd504..b686334 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs @@ -1,8 +1,9 @@ using EonaCat.logger.Managers; using EonaCat.Logger.Managers; +using EonaCatLogger.EonaCatCoreLogger.Models; using Microsoft.Extensions.Logging; using System; -using System.Text; +using System.Runtime; namespace EonaCat.Logger.Internal { @@ -48,6 +49,19 @@ namespace EonaCat.Logger.Internal } _provider.AddMessage(timestamp, message); + + var EonaCatMessage = new EonaCatLogMessage + { + DateTime = timestamp.DateTime, + Message = message, + LogType = logLevel.FromLogLevel() + }; + + if (_loggerSettings != null) + { + EonaCatMessage.Origin = string.IsNullOrWhiteSpace(_loggerSettings.LogOrigin) ? "BatchingLogger" : _loggerSettings.LogOrigin; + _loggerSettings?.OnLogEvent(EonaCatMessage); + } } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs index b66e4a2..57e6439 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs @@ -54,16 +54,21 @@ namespace EonaCat.Logger.Internal if (_currentBatch.Count > 0) { + bool isBatchWritten = false; try { await WriteMessagesAsync(_currentBatch, _cancellationTokenSource.Token); + isBatchWritten = true; } catch { // ignored } - _currentBatch.Clear(); + if (isBatchWritten) + { + _currentBatch.Clear(); + } } await IntervalAsync(_interval, _cancellationTokenSource.Token); diff --git a/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs b/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs index 80df403..9f5dbef 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs @@ -12,6 +12,7 @@ namespace EonaCatLogger.EonaCatCoreLogger.Models public DateTime DateTime { get; set; } public string Message { get; set; } public ELogType LogType { get; set; } + public string Origin { get; set; } public override string ToString() { diff --git a/EonaCat.Logger/Managers/LogManager.cs b/EonaCat.Logger/Managers/LogManager.cs index 63dc737..feb9872 100644 --- a/EonaCat.Logger/Managers/LogManager.cs +++ b/EonaCat.Logger/Managers/LogManager.cs @@ -24,10 +24,6 @@ namespace EonaCat.Logger.Managers { private readonly object _batton = new object(); private DateTime _logDate; - public event LogDelegate OnLog; - public delegate void LogDelegate(LogManager logger, ELogType logLevel, EonaCatLogMessage message); - - public ELogType LogType; private ILoggerProvider LoggerProvider { get; set; } private ILoggerFactory LoggerFactory { get; set; } @@ -119,7 +115,15 @@ namespace EonaCat.Logger.Managers IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(builder => builder.AddEonaCatFileLogger(configuration => { - configuration = Settings.FileLoggerOptions; + 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 serviceProvider = serviceCollection.BuildServiceProvider(); @@ -178,11 +182,23 @@ namespace EonaCat.Logger.Managers Write(dateTime, remainder, logType); } - var EonaCatMessage = new EonaCatLogMessage { DateTime = dateTime, Message = currentMessage, LogType = logType }; - OnLog?.Invoke(this, logType, EonaCatMessage); + var EonaCatMessage = new EonaCatLogMessage + { + DateTime = dateTime, + Message = currentMessage, + LogType = logType + }; + + if (Settings != null) + { + EonaCatMessage.Origin = string.IsNullOrWhiteSpace(Settings.LogOrigin) ? "LogManager" : Settings.LogOrigin; + Settings?.OnLogEvent(EonaCatMessage); + } + + Settings?.OnLogEvent(EonaCatMessage); } - public void Reset() => OnLog = null; + public void Reset() => Settings?.ResetLogEvent(); public LogManager(LoggerSettings settings, string serverIp, int serverPort, string id = "EonaCatLogger") { @@ -223,13 +239,16 @@ namespace EonaCat.Logger.Managers Settings.FileLoggerOptions.LogDirectory = logFolder; } - if (defaultPrefix) + if (string.IsNullOrWhiteSpace(Settings.FileLoggerOptions.FileNamePrefix)) { - Settings.FileLoggerOptions.FileNamePrefix = "EonaCat"; - } - else - { - Settings.FileLoggerOptions.FileNamePrefix = string.Empty; + if (defaultPrefix) + { + Settings.FileLoggerOptions.FileNamePrefix = "EonaCat"; + } + else + { + Settings.FileLoggerOptions.FileNamePrefix = string.Empty; + } } } diff --git a/EonaCat.Logger/Managers/LoggerSettings.cs b/EonaCat.Logger/Managers/LoggerSettings.cs index a350db0..a9c8235 100644 --- a/EonaCat.Logger/Managers/LoggerSettings.cs +++ b/EonaCat.Logger/Managers/LoggerSettings.cs @@ -1,8 +1,7 @@ -using System.IO; -using System; +using System; using System.Collections.Generic; using EonaCat.Logger.Syslog; -using System.Runtime; +using EonaCatLogger.EonaCatCoreLogger.Models; namespace EonaCat.Logger.Managers { @@ -11,6 +10,10 @@ namespace EonaCat.Logger.Managers /// public class LoggerSettings { + public event LogDelegate OnLog; + public delegate void LogDelegate(EonaCatLogMessage message); + + /// /// Header format. Provide a string that specifies how the preamble of each message should be structured. You can use variables including: /// {ts}: UTC timestamp @@ -67,11 +70,6 @@ namespace EonaCat.Logger.Managers } } - /// - /// Minimum severity required to send a message. - /// - public ESeverity MinimumSeverity { get; set; } = ESeverity.Debug; - /// /// Enable or disable use of color for console messages. /// @@ -148,6 +146,11 @@ namespace EonaCat.Logger.Managers } } + /// + /// Set the origin of where the OnLog event was inititated + /// + public string LogOrigin { get; set; } + private string _HeaderFormat = "{ts} {host} {thread} {sev}"; private string _TimestampFormat = "yyyy-MM-dd HH:mm:ss"; private bool _EnableConsole = true; @@ -169,5 +172,14 @@ namespace EonaCat.Logger.Managers } } + internal void OnLogEvent(EonaCatLogMessage eonaCatLogMessage) + { + OnLog?.Invoke(eonaCatLogMessage); + } + + internal void ResetLogEvent() + { + OnLog = null; + } } } \ No newline at end of file