diff --git a/EonaCat.Logger/DllInfo.cs b/EonaCat.Logger/DllInfo.cs index b7e2981..dcd4b8a 100644 --- a/EonaCat.Logger/DllInfo.cs +++ b/EonaCat.Logger/DllInfo.cs @@ -1,4 +1,4 @@ -namespace EonaCat.Logger.Helpers +namespace EonaCat.Logger { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. @@ -6,7 +6,7 @@ public static class DllInfo { public const string NAME = "EonaCatLogger"; - public const string VERSION = "1.0.0"; + public const string VERSION = "1.1.0"; static DllInfo() { @@ -14,10 +14,10 @@ #if DEBUG isDebug = true; #endif - VERSION_NAME = isDebug ? "DEBUG" : "RELEASE"; + VersionName = isDebug ? "DEBUG" : "RELEASE"; } - internal static string VERSION_NAME { get; } + internal static string VersionName { get; } public static string ApplicationName { get; internal set; } = "EonaCatLogger"; } diff --git a/EonaCat.Logger/EonaCat.Logger.csproj b/EonaCat.Logger/EonaCat.Logger.csproj index d954e3a..674c26f 100644 --- a/EonaCat.Logger/EonaCat.Logger.csproj +++ b/EonaCat.Logger/EonaCat.Logger.csproj @@ -2,14 +2,13 @@ - netstandard2.0; - netstandard2.1; + netstandard2.1; net5.0; net6.0; net7.0; icon.ico - 1.0.9 + 1.1.0 EonaCat (Jeroen Saey) true EonaCat (Jeroen Saey) @@ -45,7 +44,6 @@ - diff --git a/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs b/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs index 428f75c..87191d9 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Extensions/FileLoggerFactoryExtensions.cs @@ -1,11 +1,8 @@ -using EonaCat.Logger.Helpers; +using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Runtime; -namespace EonaCat.Logger.Extensions +namespace EonaCat.Logger.EonaCatCoreLogger.Extensions { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. diff --git a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerOptions.cs b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerOptions.cs index 550bb60..11e80f8 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerOptions.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerOptions.cs @@ -1,8 +1,8 @@ -using EonaCat.Logger.Internal; -using System; +using System; using System.IO; +using EonaCat.Logger.EonaCatCoreLogger.Internal; -namespace EonaCat.Logger +namespace EonaCat.Logger.EonaCatCoreLogger { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. diff --git a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs index f93889a..5e12ef2 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/FileLoggerProvider.cs @@ -1,15 +1,14 @@ -using EonaCat.Logger.Internal; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime; using System.Threading; using System.Threading.Tasks; +using EonaCat.Logger.EonaCatCoreLogger.Internal; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; -namespace EonaCat.Logger +namespace EonaCat.Logger.EonaCatCoreLogger { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. @@ -27,10 +26,10 @@ namespace EonaCat.Logger private readonly int _maxRolloverFiles; private readonly int _maxTries; private int _rollOverCount = 0; - private static readonly object _writeLock = new object(); - private static readonly object _rollOverLock = new object(); + private static readonly object WriteLock = new object(); + private static readonly object RollOverLock = new object(); private string _logFile; - private bool rollingOver; + private bool _rollingOver; /// /// The file to which log messages should be appended. @@ -95,26 +94,26 @@ namespace EonaCat.Logger File.Delete(rollOverFile); } fileInfo.CopyTo(rollOverFile); - File.WriteAllText(LogFile, string.Empty); + await File.WriteAllTextAsync(LogFile, string.Empty, cancellationToken).ConfigureAwait(false); } else { - lock (_rollOverLock) + lock (RollOverLock) { - rollingOver = true; + _rollingOver = true; MoveRolloverLogFiles(); - rollingOver = false; + _rollingOver = false; } } } } - while (rollingOver) + while (_rollingOver) { - await Task.Delay(100); + await Task.Delay(100, cancellationToken).ConfigureAwait(false); } - lock (_writeLock) + lock (WriteLock) { int tries = 0; bool completed = false; diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs index 4b2b112..374ce80 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLogger.cs @@ -1,10 +1,9 @@ -using EonaCat.logger.Managers; +using System; +using EonaCat.Logger.EonaCatCoreLogger.Models; using EonaCat.Logger.Managers; -using EonaCatLogger.EonaCatCoreLogger.Models; using Microsoft.Extensions.Logging; -using System; -namespace EonaCat.Logger.Internal +namespace EonaCat.Logger.EonaCatCoreLogger.Internal { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. @@ -49,18 +48,16 @@ namespace EonaCat.Logger.Internal _provider.AddMessage(timestamp, message); - var EonaCatMessage = new EonaCatLogMessage + var currentMessage = 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); - } + if (_loggerSettings == null) return; + currentMessage.Origin = string.IsNullOrWhiteSpace(_loggerSettings.LogOrigin) ? "BatchingLogger" : _loggerSettings.LogOrigin; + _loggerSettings?.OnLogEvent(currentMessage); } public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerOptions.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerOptions.cs index 12cf965..e083b57 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerOptions.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerOptions.cs @@ -1,6 +1,6 @@ using System; -namespace EonaCat.Logger.Internal +namespace EonaCat.Logger.EonaCatCoreLogger.Internal { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs index 57e6439..aeb3faa 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/BatchingLoggerProvider.cs @@ -1,13 +1,12 @@ -using EonaCat.Logger.Helpers; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; -namespace EonaCat.Logger.Internal +namespace EonaCat.Logger.EonaCatCoreLogger.Internal { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. @@ -39,9 +38,9 @@ namespace EonaCat.Logger.Internal protected abstract Task WriteMessagesAsync(IEnumerable messages, CancellationToken token); - private async Task ProcessLogQueue(object state) + private async Task ProcessLogQueueAsync(object state) { - await WriteMessagesAsync(new List { new LogMessage { Message = $"[{DllInfo.ApplicationName}] {DllInfo.ApplicationName} started.{Environment.NewLine}", Timestamp = DateTimeOffset.Now } }, _cancellationTokenSource.Token); + await WriteMessagesAsync(new List { new LogMessage { Message = $"[{DllInfo.ApplicationName}] {DllInfo.ApplicationName} started.{Environment.NewLine}", Timestamp = DateTimeOffset.Now } }, _cancellationTokenSource.Token).ConfigureAwait(false); while (!_cancellationTokenSource.IsCancellationRequested) { int limit = _batchSize <= 0 ? int.MaxValue : _batchSize; @@ -57,7 +56,7 @@ namespace EonaCat.Logger.Internal bool isBatchWritten = false; try { - await WriteMessagesAsync(_currentBatch, _cancellationTokenSource.Token); + await WriteMessagesAsync(_currentBatch, _cancellationTokenSource.Token).ConfigureAwait(false); isBatchWritten = true; } catch @@ -71,9 +70,9 @@ namespace EonaCat.Logger.Internal } } - await IntervalAsync(_interval, _cancellationTokenSource.Token); + await IntervalAsync(_interval, _cancellationTokenSource.Token).ConfigureAwait(false); } - await WriteMessagesAsync(new List { new LogMessage { Message = $"[{DllInfo.ApplicationName}] {DllInfo.ApplicationName} stopped.{Environment.NewLine}", Timestamp = DateTimeOffset.Now } }, _cancellationTokenSource.Token); + await WriteMessagesAsync(new List { new LogMessage { Message = $"[{DllInfo.ApplicationName}] {DllInfo.ApplicationName} stopped.{Environment.NewLine}", Timestamp = DateTimeOffset.Now } }, _cancellationTokenSource.Token).ConfigureAwait(false); } protected virtual Task IntervalAsync(TimeSpan interval, CancellationToken cancellationToken) @@ -92,18 +91,18 @@ namespace EonaCat.Logger.Internal _cancellationTokenSource = new CancellationTokenSource(); _outputTask = Task.Factory.StartNew( - ProcessLogQueue, + ProcessLogQueueAsync, null, TaskCreationOptions.LongRunning); } - private void Stop() + private async Task StopAsync() { _cancellationTokenSource.Cancel(); try { - _outputTask.Wait(_interval); + await _outputTask.ConfigureAwait(false); } catch (TaskCanceledException) { @@ -115,7 +114,9 @@ namespace EonaCat.Logger.Internal public void Dispose() { - Stop(); +#pragma warning disable CS4014 + StopAsync(); +#pragma warning restore CS4014 GC.SuppressFinalize(this); } diff --git a/EonaCat.Logger/EonaCatCoreLogger/Internal/LogMessage.cs b/EonaCat.Logger/EonaCatCoreLogger/Internal/LogMessage.cs index 378dd23..8100fac 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Internal/LogMessage.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Internal/LogMessage.cs @@ -1,6 +1,6 @@ using System; -namespace EonaCat.Logger.Internal +namespace EonaCat.Logger.EonaCatCoreLogger.Internal { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. diff --git a/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs b/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs index 9f5dbef..dffe5e3 100644 --- a/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs +++ b/EonaCat.Logger/EonaCatCoreLogger/Models/EonaCatLogMessage.cs @@ -1,8 +1,7 @@ -using EonaCat.Logger; +using System; using EonaCat.Logger.Helpers; -using System; -namespace EonaCatLogger.EonaCatCoreLogger.Models +namespace EonaCat.Logger.EonaCatCoreLogger.Models { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. diff --git a/EonaCat.Logger/Extensions/OffsetStream.cs b/EonaCat.Logger/Extensions/OffsetStream.cs index 791e285..7a25aa1 100644 --- a/EonaCat.Logger/Extensions/OffsetStream.cs +++ b/EonaCat.Logger/Extensions/OffsetStream.cs @@ -1,14 +1,14 @@ using System; using System.IO; -namespace EonaCat.Extensions +namespace EonaCat.Logger.Extensions { // This file is part of the EonaCat project(s) which is released under the Apache License. // See the LICENSE file or go to https://EonaCat.com/License for full license details. public class OffsetStream : Stream { - private const int BUFFERSIZE = 4096; + private const int BufferSize = 4096; public OffsetStream(Stream stream, long offset = 0, long length = 0, bool readOnly = false, bool ownStream = false) { @@ -230,7 +230,7 @@ namespace EonaCat.Extensions public void WriteTo(Stream stream) { - WriteTo(stream, BUFFERSIZE); + WriteTo(stream, BufferSize); } public void WriteTo(Stream stream, int bufferSize) diff --git a/EonaCat.Logger/Helpers/EnumHelper.cs b/EonaCat.Logger/Helpers/EnumHelper.cs index 38ad567..82b54f0 100644 --- a/EonaCat.Logger/Helpers/EnumHelper.cs +++ b/EonaCat.Logger/Helpers/EnumHelper.cs @@ -27,7 +27,7 @@ namespace EonaCat.Logger.Helpers public static Dictionary Names { get; } - public static Dictionary Values { get; private set; } + public static Dictionary Values { get; } public static string ToString(T value) { diff --git a/EonaCat.Logger/Managers/LogHelper.cs b/EonaCat.Logger/Managers/LogHelper.cs index 9da7724..5bd3da2 100644 --- a/EonaCat.Logger/Managers/LogHelper.cs +++ b/EonaCat.Logger/Managers/LogHelper.cs @@ -1,23 +1,20 @@ -using EonaCat.Logger; -using EonaCat.Logger.Managers; -using EonaCat.Logger.Syslog; -using Microsoft.Extensions.Logging; -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; +using EonaCat.Logger.Syslog; +using Microsoft.Extensions.Logging; -namespace EonaCat.logger.Managers +namespace EonaCat.Logger.Managers { public static class LogHelper { - private static readonly object _fileLock = new object(); + private static readonly object FileLock = new object(); /// /// Format a message with the specified header @@ -204,7 +201,7 @@ namespace EonaCat.logger.Managers internal static void SendFile(ILogger logger, LoggerSettings settings, ELogType logType, string message, int maxTries = 3) { - lock (_fileLock) + lock (FileLock) { if (logger == null) return; if (settings == null) return; diff --git a/EonaCat.Logger/Managers/LogManager.cs b/EonaCat.Logger/Managers/LogManager.cs index 8836bd5..14c7c01 100644 --- a/EonaCat.Logger/Managers/LogManager.cs +++ b/EonaCat.Logger/Managers/LogManager.cs @@ -1,9 +1,6 @@ -using EonaCat.logger.Managers; -using EonaCat.Logger.Exceptions; -using EonaCat.Logger.Extensions; +using EonaCat.Logger.Exceptions; using EonaCat.Logger.Helpers; using EonaCat.Logger.Syslog; -using EonaCatLogger.EonaCatCoreLogger.Models; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; @@ -11,6 +8,9 @@ using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; +using EonaCat.Logger.EonaCatCoreLogger; +using EonaCat.Logger.EonaCatCoreLogger.Extensions; +using EonaCat.Logger.EonaCatCoreLogger.Models; namespace EonaCat.Logger.Managers { @@ -37,8 +37,8 @@ namespace EonaCat.Logger.Managers private bool _disposed; private static LogManager _instance; private LoggerSettings _settings; - private CancellationTokenSource _TokenSource = new CancellationTokenSource(); - private CancellationToken _Token; + private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource(); + private CancellationToken _token; /// /// Default Logger Instance with it's default configuration @@ -84,7 +84,7 @@ namespace EonaCat.Logger.Managers if (disposing) { StopLogging(); - _TokenSource.Cancel(); + _tokenSource.Cancel(); } _disposed = true; @@ -92,7 +92,7 @@ namespace EonaCat.Logger.Managers private void StartNewLog() { - if (_TokenSource.IsCancellationRequested) + if (_tokenSource.IsCancellationRequested) { return; } @@ -180,7 +180,7 @@ namespace EonaCat.Logger.Managers Write(dateTime, remainder, logType, writeToConsole); } - var EonaCatMessage = new EonaCatLogMessage + var logMessage = new EonaCatLogMessage { DateTime = dateTime, Message = currentMessage, @@ -189,11 +189,11 @@ namespace EonaCat.Logger.Managers if (Settings != null) { - EonaCatMessage.Origin = string.IsNullOrWhiteSpace(Settings.LogOrigin) ? "LogManager" : Settings.LogOrigin; - Settings?.OnLogEvent(EonaCatMessage); + logMessage.Origin = string.IsNullOrWhiteSpace(Settings.LogOrigin) ? "LogManager" : Settings.LogOrigin; + Settings?.OnLogEvent(logMessage); } - Settings?.OnLogEvent(EonaCatMessage); + Settings?.OnLogEvent(logMessage); } public void Reset() => Settings?.ResetLogEvent(); @@ -252,7 +252,7 @@ namespace EonaCat.Logger.Managers private void SetupLogManager() { - _Token = _TokenSource.Token; + _token = _tokenSource.Token; AppDomain.CurrentDomain.ProcessExit += ProcessExit; Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress); @@ -274,7 +274,7 @@ namespace EonaCat.Logger.Managers { IsRunning = false; Write(DateTime.Now, $"{DllInfo.ApplicationName} stopped."); - Task.Delay(500); + Task.Delay(500, _token); } public LogManager(string logFolder = null, bool defaultPrefix = true) diff --git a/EonaCat.Logger/Managers/LoggerSettings.cs b/EonaCat.Logger/Managers/LoggerSettings.cs index a9c8235..c5bada9 100644 --- a/EonaCat.Logger/Managers/LoggerSettings.cs +++ b/EonaCat.Logger/Managers/LoggerSettings.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; +using EonaCat.Logger.EonaCatCoreLogger; +using EonaCat.Logger.EonaCatCoreLogger.Models; using EonaCat.Logger.Syslog; -using EonaCatLogger.EonaCatCoreLogger.Models; namespace EonaCat.Logger.Managers { @@ -27,12 +28,12 @@ namespace EonaCat.Logger.Managers { get { - return _HeaderFormat; + return _headerFormat; } set { - if (string.IsNullOrEmpty(value)) _HeaderFormat = ""; - else _HeaderFormat = value; + if (string.IsNullOrEmpty(value)) _headerFormat = ""; + else _headerFormat = value; } } @@ -43,12 +44,12 @@ namespace EonaCat.Logger.Managers { get { - return _TimestampFormat; + return _timestampFormat; } set { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(HeaderFormat)); - _TimestampFormat = value; + _timestampFormat = value; } } @@ -61,12 +62,12 @@ namespace EonaCat.Logger.Managers { get { - return _EnableConsole; + return _enableConsole; } set { - if (value) _EnableConsole = ConsoleExists(); - else _EnableConsole = false; + if (value) _enableConsole = ConsoleExists(); + else _enableConsole = false; } } @@ -137,12 +138,12 @@ namespace EonaCat.Logger.Managers { get { - return _MaxMessageLength; + return _maxMessageLength; } set { if (value < 32) throw new ArgumentException("Maximum message length must be at least 32."); - _MaxMessageLength = value; + _maxMessageLength = value; } } @@ -151,10 +152,10 @@ namespace EonaCat.Logger.Managers /// 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; - private int _MaxMessageLength = 1024; + 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();