Skip to content

Commit

Permalink
added Microsoft.Extensions.Logging.ILogger and removed self implement…
Browse files Browse the repository at this point in the history
…ed ILogger file
  • Loading branch information
bezzad committed Sep 18, 2024
1 parent 8b2f27e commit f130752
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/Downloader.Test/Downloader.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
50 changes: 37 additions & 13 deletions src/Downloader.Test/Helper/FileLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand All @@ -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<string> LogQueue;
protected string LogPath;
protected StreamWriter LogStream;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>(TState state) where TState : notnull
{
throw new NotImplementedException();
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> 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);
}
}
2 changes: 1 addition & 1 deletion src/Downloader/AbstractDownloadService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/ChunkDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Downloader.Extensions.Helpers;
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/ConcurrentPacketBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Concurrent;
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/ConcurrentStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/DownloadPackage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
Expand Down
1 change: 0 additions & 1 deletion src/Downloader/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
1 change: 1 addition & 0 deletions src/Downloader/Downloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
17 changes: 0 additions & 17 deletions src/Downloader/Extensions/Logging/ILogger.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Downloader/IDownloadService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.ComponentModel;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader/TaskStateManagement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Downloader.Sample/Program.Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Downloader.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ShellProgressBar;
using System;
Expand Down

0 comments on commit f130752

Please sign in to comment.