From f1307525f26e1b21274393716ae06d5dc4b93019 Mon Sep 17 00:00:00 2001 From: bezzad Date: Wed, 18 Sep 2024 16:32:27 +0330 Subject: [PATCH] added Microsoft.Extensions.Logging.ILogger and removed self implemented ILogger file --- src/Downloader.Test/Downloader.Test.csproj | 1 + src/Downloader.Test/Helper/FileLogger.cs | 50 ++++++++++++++----- src/Downloader/AbstractDownloadService.cs | 2 +- src/Downloader/ChunkDownloader.cs | 2 +- src/Downloader/ConcurrentPacketBuffer.cs | 2 +- src/Downloader/ConcurrentStream.cs | 2 +- src/Downloader/DownloadPackage.cs | 2 +- src/Downloader/DownloadService.cs | 1 - src/Downloader/Downloader.csproj | 1 + src/Downloader/Extensions/Logging/ILogger.cs | 17 ------- src/Downloader/IDownloadService.cs | 2 +- src/Downloader/TaskStateManagement.cs | 2 +- .../Downloader.Sample/Program.Config.cs | 2 +- src/Samples/Downloader.Sample/Program.cs | 2 +- 14 files changed, 48 insertions(+), 40 deletions(-) delete mode 100644 src/Downloader/Extensions/Logging/ILogger.cs diff --git a/src/Downloader.Test/Downloader.Test.csproj b/src/Downloader.Test/Downloader.Test.csproj index 2045aaf..06945fe 100644 --- a/src/Downloader.Test/Downloader.Test.csproj +++ b/src/Downloader.Test/Downloader.Test.csproj @@ -29,6 +29,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Downloader.Test/Helper/FileLogger.cs b/src/Downloader.Test/Helper/FileLogger.cs index 11989aa..a239254 100644 --- a/src/Downloader.Test/Helper/FileLogger.cs +++ b/src/Downloader.Test/Helper/FileLogger.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -12,7 +13,7 @@ namespace Downloader.Extensions.Logging; public class FileLogger : ILogger, IDisposable { private volatile bool _disposed; - private SemaphoreSlim _semaphore; + private readonly SemaphoreSlim _semaphore; protected readonly ConcurrentQueue LogQueue; protected string LogPath; protected StreamWriter LogStream; @@ -41,51 +42,51 @@ public FileLogger(string logPath) public void LogDebug(string message) { - Log(nameof(LogDebug), message); + Log(LogLevel.Information, message); } public void LogInfo(string message) { - Log(nameof(LogInfo), message); + Log(LogLevel.Information, message); } public void LogWarning(string message) { - Log(nameof(LogWarning), message); + Log(LogLevel.Warning, message); } public void LogError(string message) { - Log(nameof(LogError), message); + Log(LogLevel.Error, message); } public void LogError(Exception exception, string message) { - Log(nameof(LogError), message, exception); + Log(LogLevel.Error, message, exception); } public void LogCritical(string message) { - Log(nameof(LogCritical), message); + Log(LogLevel.Critical, message); } public void LogCritical(Exception exception, string message) { - Log(nameof(LogCritical), message, exception); + Log(LogLevel.Critical, message, exception); } - protected void Log(string logType, string message, Exception exception = null) + protected void Log(LogLevel logLevel, string message, Exception exception = null) { if (!_disposed) { - LogQueue.Enqueue(Formatter(logType, message, exception)); + LogQueue.Enqueue(Formatter(logLevel, message, exception)); _semaphore.Release(); } } - public virtual string Formatter(string logType, string message, Exception exception) + public virtual string Formatter(LogLevel logLevel, string message, Exception exception) { - var log = $"{DateTime.Now:s} | {logType} | {message}"; + var log = $"{DateTime.Now:s} | {logLevel} | {message}"; if (exception is not null) { log += " | " + exception.Message + ": " + exception.StackTrace; @@ -138,4 +139,27 @@ private static Stream CreateFile(string filename) return new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete); } + + public IDisposable BeginScope(TState state) where TState : notnull + { + throw new NotImplementedException(); + } + + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + + var logMessage = formatter(state, exception); + var logEntry = $"{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ} [{logLevel}] {logMessage}{Environment.NewLine}"; + + Log(logLevel, logEntry, exception); + } } diff --git a/src/Downloader/AbstractDownloadService.cs b/src/Downloader/AbstractDownloadService.cs index 2ef3f6b..4c439c7 100644 --- a/src/Downloader/AbstractDownloadService.cs +++ b/src/Downloader/AbstractDownloadService.cs @@ -1,5 +1,5 @@ using Downloader.Extensions.Helpers; -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/src/Downloader/ChunkDownloader.cs b/src/Downloader/ChunkDownloader.cs index 3b95087..0810438 100644 --- a/src/Downloader/ChunkDownloader.cs +++ b/src/Downloader/ChunkDownloader.cs @@ -1,5 +1,5 @@ using Downloader.Extensions.Helpers; -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.ComponentModel; using System.IO; diff --git a/src/Downloader/ConcurrentPacketBuffer.cs b/src/Downloader/ConcurrentPacketBuffer.cs index 17a75b3..a1426ac 100644 --- a/src/Downloader/ConcurrentPacketBuffer.cs +++ b/src/Downloader/ConcurrentPacketBuffer.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.Collections; using System.Collections.Concurrent; diff --git a/src/Downloader/ConcurrentStream.cs b/src/Downloader/ConcurrentStream.cs index 8eabcd9..be96e28 100644 --- a/src/Downloader/ConcurrentStream.cs +++ b/src/Downloader/ConcurrentStream.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.IO; using System.Threading; diff --git a/src/Downloader/DownloadPackage.cs b/src/Downloader/DownloadPackage.cs index 50c5c08..415891d 100644 --- a/src/Downloader/DownloadPackage.cs +++ b/src/Downloader/DownloadPackage.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading.Tasks; diff --git a/src/Downloader/DownloadService.cs b/src/Downloader/DownloadService.cs index 89af2b5..c2b1184 100644 --- a/src/Downloader/DownloadService.cs +++ b/src/Downloader/DownloadService.cs @@ -62,7 +62,6 @@ private async Task SendDownloadCompletionSignal(DownloadStatus state, Exception Package.IsSaveComplete = state == DownloadStatus.Completed; Status = state; await (Package?.Storage?.FlushAsync() ?? Task.FromResult(0)).ConfigureAwait(false); - await (_logger?.FlushAsync() ?? Task.FromResult(0)).ConfigureAwait(false); OnDownloadFileCompleted(new AsyncCompletedEventArgs(error, isCancelled, Package)); } diff --git a/src/Downloader/Downloader.csproj b/src/Downloader/Downloader.csproj index aa8ac64..7d9e0fd 100644 --- a/src/Downloader/Downloader.csproj +++ b/src/Downloader/Downloader.csproj @@ -71,6 +71,7 @@ + diff --git a/src/Downloader/Extensions/Logging/ILogger.cs b/src/Downloader/Extensions/Logging/ILogger.cs deleted file mode 100644 index 030766d..0000000 --- a/src/Downloader/Extensions/Logging/ILogger.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Downloader.Extensions.Logging; - -public interface ILogger -{ - void LogDebug(string message); - void LogInfo(string message); - void LogWarning(string message); - void LogError(string message); - void LogError(Exception exception, string message); - void LogCritical(string message); - void LogCritical(Exception exception, string message); - string Formatter(string logType, string message, Exception exception); - Task FlushAsync(); -} diff --git a/src/Downloader/IDownloadService.cs b/src/Downloader/IDownloadService.cs index 6065d77..cfa9661 100644 --- a/src/Downloader/IDownloadService.cs +++ b/src/Downloader/IDownloadService.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.ComponentModel; using System.IO; diff --git a/src/Downloader/TaskStateManagement.cs b/src/Downloader/TaskStateManagement.cs index 715327e..699f230 100644 --- a/src/Downloader/TaskStateManagement.cs +++ b/src/Downloader/TaskStateManagement.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.Collections.Concurrent; using System.Runtime.CompilerServices; diff --git a/src/Samples/Downloader.Sample/Program.Config.cs b/src/Samples/Downloader.Sample/Program.Config.cs index bc2b6ad..5037580 100644 --- a/src/Samples/Downloader.Sample/Program.Config.cs +++ b/src/Samples/Downloader.Sample/Program.Config.cs @@ -15,7 +15,7 @@ private static DownloadConfiguration GetDownloadConfiguration() ChunkCount = 8, // file parts to download, default value is 1 MaximumBytesPerSecond = 1024 * 1024 * 10, // download speed limited to 10MB/s, default values is zero or unlimited MaxTryAgainOnFailover = 5, // the maximum number of times to fail - MaximumMemoryBufferBytes = 1024 * 1024 * 200, // release memory buffer after each 200MB + MaximumMemoryBufferBytes = 1024 * 1024 * 500, // release memory buffer after each 500MB ParallelDownload = true, // download parts of file as parallel or not. Default value is false ParallelCount = 8, // number of parallel downloads. The default value is the same as the chunk count Timeout = 3000, // timeout (millisecond) per stream block reader, default value is 1000 diff --git a/src/Samples/Downloader.Sample/Program.cs b/src/Samples/Downloader.Sample/Program.cs index cf4be9e..574f528 100644 --- a/src/Samples/Downloader.Sample/Program.cs +++ b/src/Samples/Downloader.Sample/Program.cs @@ -1,4 +1,4 @@ -using Downloader.Extensions.Logging; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using ShellProgressBar; using System;