Skip to content

Commit

Permalink
added logger to sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Dec 8, 2023
1 parent 600c8c7 commit fa6f839
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Downloader.DummyHttpServer;
using Downloader.Test.Helper;
using Newtonsoft.Json;
using System;
using System.IO;
Expand All @@ -9,6 +8,7 @@
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using FileLogger = Downloader.Extensions.Logging.FileLogger;

namespace Downloader.Test.IntegrationTests;

Expand Down Expand Up @@ -805,11 +805,11 @@ public async Task DownloadBigFileOnDisk()
{
// arrange
var totalSize = 1024 * 1024 * 100; // 100MB
//Downloader.AddLogger(new FileLogger($"D:\\TestDownload\\DownloadBigFileOnDisk_{DateTime.Now.ToString("yyyyMMdd.HHmmss")}.log"));
Config.ChunkCount = 8;
Config.ParallelCount = 8;
Config.MaximumBytesPerSecond = 0;
URL = DummyFileHelper.GetFileWithNameUrl(Filename, totalSize);
//Downloader.AddLogger(FileLogger.Factory("D:\\TestDownload"));
var actualFile = DummyData.GenerateOrderedBytes(totalSize);

// act
Expand Down Expand Up @@ -851,13 +851,13 @@ public async Task DownloadBigFileWithMemoryLimitationOnDisk()
{
// arrange
var totalSize = 1024 * 1024 * 1024; // 1GB
//Downloader.AddLogger(new FileLogger($"D:\\TestDownload\\DownloadBigFileOnDisk_{DateTime.Now.ToString("yyyyMMdd.HHmmss")}.log"));
byte fillByte = 123;
Config.ChunkCount = 16;
Config.ParallelCount = 16;
Config.MaximumBytesPerSecond = 0;
Config.MaximumMemoryBufferBytes = 1024 * 1024 * 100; // 100MB
URL = DummyFileHelper.GetFileWithNameUrl(Filename, totalSize, fillByte);
//Downloader.AddLogger(FileLogger.Factory("D:\\TestDownload"));

// act
await Downloader.DownloadFileTaskAsync(URL, FilePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
using Downloader.Extensions.Logging;
using System;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Downloader.Test.Helper;
namespace Downloader.Extensions.Logging;

internal class FileLogger : ILogger, IDisposable
public class FileLogger : ILogger, IDisposable
{
private volatile bool _disposed = false;
private SemaphoreSlim _semaphore = new SemaphoreSlim(0);
private volatile bool _disposed;
private SemaphoreSlim _semaphore;
protected readonly ConcurrentQueue<string> LogQueue;
protected string LogPath;
protected StreamWriter LogStream;

public static FileLogger Factory(string logPath, [CallerMemberName] string logName = default)
{
var filename = logName + "_" + DateTime.Now.ToString("yyyyMMdd.HHmmss") + ".log";
return new FileLogger(Path.Combine(logPath, filename));
}

public FileLogger(string logPath)
{
_semaphore = new SemaphoreSlim(0);
LogQueue = new ConcurrentQueue<string>();
LogPath = logPath;
LogStream = new StreamWriter(FileHelper.CreateFile(logPath));
Expand Down Expand Up @@ -105,7 +112,6 @@ private async Task Writer()
public void Dispose()
{
_disposed = true;
LogQueue.Clear();
LogStream?.Dispose();
LogStream = null;
}
Expand All @@ -117,6 +123,6 @@ public async Task FlushAsync()
await Task.Delay(100);
}

await (LogStream?.FlushAsync() ?? Task.CompletedTask).ConfigureAwait(false);
await (LogStream?.FlushAsync() ?? Task.FromResult(0)).ConfigureAwait(false);
}
}
2 changes: 1 addition & 1 deletion src/Downloader/Extensions/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Downloader.Extensions.Logging;

public interface ILogger
public interface ILogger
{
void Debug(string message);
void Info(string message);
Expand Down
Loading

0 comments on commit fa6f839

Please sign in to comment.