Skip to content

Commit

Permalink
added logging to validate data in sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Dec 23, 2023
1 parent 2084c85 commit 5642f41
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Downloader.Extensions.Logging;
using Newtonsoft.Json;
using ShellProgressBar;
using System;
using System.Collections.Concurrent;
Expand All @@ -23,6 +24,7 @@ public partial class Program
private static IDownloadService CurrentDownloadService;
private static DownloadConfiguration CurrentDownloadConfiguration;
private static CancellationTokenSource CancelAllTokenSource;
private static ILogger Logger;

private static async Task Main()
{
Expand Down Expand Up @@ -139,12 +141,14 @@ private static async Task<IDownloadService> DownloadFile(DownloadItem downloadIt
CurrentDownloadService = CreateDownloadService(CurrentDownloadConfiguration);
if (string.IsNullOrWhiteSpace(downloadItem.FileName))
{
CurrentDownloadService.AddLogger(FileLogger.Factory(downloadItem.FolderPath));
Logger = FileLogger.Factory(downloadItem.FolderPath);
CurrentDownloadService.AddLogger(Logger);
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, new DirectoryInfo(downloadItem.FolderPath)).ConfigureAwait(false);
}
else
{
CurrentDownloadService.AddLogger(FileLogger.Factory(downloadItem.FolderPath, Path.GetFileName(downloadItem.FileName)));
Logger = FileLogger.Factory(downloadItem.FolderPath, Path.GetFileName(downloadItem.FileName));
CurrentDownloadService.AddLogger(Logger);
await CurrentDownloadService.DownloadFileTaskAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);
}

Expand All @@ -153,7 +157,9 @@ private static async Task<IDownloadService> DownloadFile(DownloadItem downloadIt
var isValid = await ValidateDataAsync(CurrentDownloadService.Package.FileName, CurrentDownloadService.Package.TotalFileSize).ConfigureAwait(false);
if (!isValid)
{
throw new InvalidDataException("Downloaded data is invalid: " + CurrentDownloadService.Package.FileName);
var message = "Downloaded data is invalid: " + CurrentDownloadService.Package.FileName;
Logger?.LogCritical(message);
throw new InvalidDataException(message);
}
}

Expand All @@ -168,6 +174,7 @@ private static async Task<bool> ValidateDataAsync(string filename, long size)
var next = stream.ReadByte();
if (next != i % 256)
{
Logger?.LogWarning($"Sample.Program.ValidateDataAsync(): Data at index [{i}] of `{filename}` is `{next}`, expectation is `{i % 256}`");
return false;
}
}
Expand Down

0 comments on commit 5642f41

Please sign in to comment.