add logging

This commit is contained in:
Collin M. Barrett 2019-06-27 13:32:25 -05:00
parent f44d6dd355
commit be3a8d1eaf
4 changed files with 24 additions and 11 deletions

View file

@ -35,6 +35,8 @@
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.3" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="RestSharp" Version="106.6.9" />
</ItemGroup>

View file

@ -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<string, Func<ListInfo, IRequest>> DownloadReq
{".zip", l => throw new NotImplementedException()}
};
private readonly ILogger<Handler> _logger;
private readonly IMediator _mediator;
public Handler(IMediator mediator)
public Handler(ILogger<Handler> 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}.");
}
}
}

View file

@ -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<Command>
{
private readonly HttpClient _httpClient;
private readonly ILogger<Handler> _logger;
public Handler(HttpClient httpClient)
public Handler(ILogger<Handler> 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}.");
}
}
}

View file

@ -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<FilterListsApiClient>().AsImplementedInterfaces().SingleInstance();
containerBuilder.RegisterType<HttpClient>().SingleInstance();