From 8ef5065c139428d9cb9d251f9509ab83a1514627 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Thu, 27 Jun 2019 13:32:25 -0500 Subject: [PATCH] add logging --- src/FilterLists.Agent/FilterLists.Agent.csproj | 2 ++ .../ListArchiver/DownloadList.cs | 16 +++++++++++----- .../DownloadTxt.cs | 15 +++++++++------ src/FilterLists.Agent/Program.cs | 2 ++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/FilterLists.Agent/FilterLists.Agent.csproj b/src/FilterLists.Agent/FilterLists.Agent.csproj index 08a11096c..ceace7a57 100644 --- a/src/FilterLists.Agent/FilterLists.Agent.csproj +++ b/src/FilterLists.Agent/FilterLists.Agent.csproj @@ -35,6 +35,8 @@ + + diff --git a/src/FilterLists.Agent/ListArchiver/DownloadList.cs b/src/FilterLists.Agent/ListArchiver/DownloadList.cs index f6567fc00..7403826bd 100644 --- a/src/FilterLists.Agent/ListArchiver/DownloadList.cs +++ b/src/FilterLists.Agent/ListArchiver/DownloadList.cs @@ -6,6 +6,7 @@ using FilterLists.Agent.Entities; using FilterLists.Agent.ListArchiver.DownloadRequestsByFileExtension; using MediatR; +using Microsoft.Extensions.Logging; namespace FilterLists.Agent.ListArchiver { @@ -53,10 +54,12 @@ private static readonly Dictionary> DownloadReq {".zip", l => throw new NotImplementedException()} }; + private readonly ILogger _logger; private readonly IMediator _mediator; - public Handler(IMediator mediator) + public Handler(ILogger logger, IMediator mediator) { + _logger = logger; _mediator = mediator; } @@ -68,16 +71,19 @@ protected override async Task Handle(Command request, CancellationToken cancella if (DownloadRequestsByFileExtension.ContainsKey(extension)) await _mediator.Send(DownloadRequestsByFileExtension[extension].Invoke(request.ListInfo), cancellationToken); - //TODO: handle and/or log unrecognized extension + _logger.LogWarning( + $"File extension not recognized for list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}."); //TODO: upsert into MariaDB Rules table https://stackoverflow.com/questions/15271202/mysql-load-data-infile-with-on-duplicate-key-update } catch (NotImplementedException) { - //TODO: log + _logger.LogWarning( + $"File extension not supported for list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}."); } - catch (ArgumentException) + catch (ArgumentException ex) { - //TODO: log + _logger.LogError(ex, + $"Could not determine the file extension for list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}."); } } } diff --git a/src/FilterLists.Agent/ListArchiver/DownloadRequestsByFileExtension/DownloadTxt.cs b/src/FilterLists.Agent/ListArchiver/DownloadRequestsByFileExtension/DownloadTxt.cs index 1e5f0bbdc..8f0e9fb10 100644 --- a/src/FilterLists.Agent/ListArchiver/DownloadRequestsByFileExtension/DownloadTxt.cs +++ b/src/FilterLists.Agent/ListArchiver/DownloadRequestsByFileExtension/DownloadTxt.cs @@ -1,10 +1,10 @@ -using System.Diagnostics; -using System.IO; +using System.IO; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using FilterLists.Agent.Entities; using MediatR; +using Microsoft.Extensions.Logging; namespace FilterLists.Agent.ListArchiver.DownloadRequestsByFileExtension { @@ -23,15 +23,17 @@ public Command(ListInfo listInfo) public class Handler : AsyncRequestHandler { private readonly HttpClient _httpClient; + private readonly ILogger _logger; - public Handler(HttpClient httpClient) + public Handler(ILogger logger, HttpClient httpClient) { + _logger = logger; _httpClient = httpClient; } protected override async Task Handle(Command request, CancellationToken cancellationToken) { - Debug.WriteLine($"Downloading list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}..."); + _logger.LogInformation($"Downloading list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}..."); try { using (var result = await _httpClient.GetAsync(request.ListInfo.ViewUrl, cancellationToken)) @@ -45,9 +47,10 @@ protected override async Task Handle(Command request, CancellationToken cancella } } } - catch (HttpRequestException) + catch (HttpRequestException ex) { - //TODO: log + _logger.LogError(ex, + $"Error downloading list {request.ListInfo.Id} from {request.ListInfo.ViewUrl}."); } } } diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index af6df94d6..2fa52e08e 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -7,6 +7,7 @@ using FilterLists.Agent.ListArchiver; using MediatR; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace FilterLists.Agent { @@ -30,6 +31,7 @@ private static void RegisterServices() var containerBuilder = new ContainerBuilder(); // register Agent services + serviceCollection.AddLogging(b => b.AddConsole()); serviceCollection.AddMediatR(typeof(Program).Assembly); containerBuilder.RegisterType().AsImplementedInterfaces().SingleInstance(); containerBuilder.RegisterType().SingleInstance();