From 7f2cbf1a906e9082f88b4f3a5d622aa4cde3c03a Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Tue, 13 Feb 2024 10:35:56 +0100 Subject: [PATCH] add ilogger --- .../Helpers/LoggerHelper.cs | 37 +++++++++++++++++++ .../Nager.PublicSuffix.UnitTest.csproj | 1 + .../RuleProviderTest.cs | 5 ++- .../CacheProviders/ICacheProvider.cs | 4 +- .../LocalFileSystemCacheProvider.cs | 6 +-- .../Nager.PublicSuffix.csproj | 1 + .../RuleProviders/WebRuleProvider.cs | 25 +++++++++++-- 7 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/Nager.PublicSuffix.UnitTest/Helpers/LoggerHelper.cs diff --git a/src/Nager.PublicSuffix.UnitTest/Helpers/LoggerHelper.cs b/src/Nager.PublicSuffix.UnitTest/Helpers/LoggerHelper.cs new file mode 100644 index 0000000..e610ece --- /dev/null +++ b/src/Nager.PublicSuffix.UnitTest/Helpers/LoggerHelper.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Logging; +using Moq; +using System; +using System.Diagnostics; + +namespace Nager.PublicSuffix.UnitTest.Helpers +{ + public static class LoggerHelper + { + public static Mock> GetLogger() + { + var logger = new Mock>(); + + logger.Setup(x => x.Log( + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + (Func)It.IsAny())) + .Callback(new InvocationAction(invocation => + { + var logLevel = (LogLevel)invocation.Arguments[0]; // The first two will always be whatever is specified in the setup above + var eventId = (EventId)invocation.Arguments[1]; // so I'm not sure you would ever want to actually use them + var state = invocation.Arguments[2]; + var exception = (Exception)invocation.Arguments[3]; + var formatter = invocation.Arguments[4]; + + var invokeMethod = formatter.GetType().GetMethod("Invoke"); + var logMessage = (string?)invokeMethod?.Invoke(formatter, new[] { state, exception }); + + Trace.WriteLine($"{logLevel} - {logMessage}"); + })); + + return logger; + } + } +} diff --git a/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj b/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj index 96432b3..3e08672 100644 --- a/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj +++ b/src/Nager.PublicSuffix.UnitTest/Nager.PublicSuffix.UnitTest.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs b/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs index 0eeea0d..369fd8f 100644 --- a/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs +++ b/src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs @@ -2,6 +2,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.CacheProviders; using Nager.PublicSuffix.RuleProviders; +using Nager.PublicSuffix.UnitTest.Helpers; using System.Net.Http; using System.Threading.Tasks; @@ -13,13 +14,15 @@ public class RuleProviderTest [TestMethod] public async Task WebTldRuleProviderTest() { + var loggerMock = LoggerHelper.GetLogger(); + var builder = new ConfigurationBuilder(); using var httpClient = new HttpClient(); var configuration = builder.Build(); var cacheProvider = new LocalFileSystemCacheProvider(); - var webRuleProvider = new WebRuleProvider(configuration, cacheProvider, httpClient); + var webRuleProvider = new WebRuleProvider(loggerMock.Object, configuration, cacheProvider, httpClient); var domainDataStructure = await webRuleProvider.BuildAsync(); Assert.IsNotNull(domainDataStructure); } diff --git a/src/Nager.PublicSuffix/CacheProviders/ICacheProvider.cs b/src/Nager.PublicSuffix/CacheProviders/ICacheProvider.cs index d123ca9..dfb80d2 100644 --- a/src/Nager.PublicSuffix/CacheProviders/ICacheProvider.cs +++ b/src/Nager.PublicSuffix/CacheProviders/ICacheProvider.cs @@ -8,13 +8,13 @@ namespace Nager.PublicSuffix.CacheProviders public interface ICacheProvider { /// - /// Get the data of the cache file + /// Get the data from the cache /// /// Returns null if the cache has expired Task GetAsync(); /// - /// Create or update the cache file + /// Store data in the cache /// /// /// diff --git a/src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs b/src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs index 861fd4b..65ade99 100644 --- a/src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs +++ b/src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs @@ -36,7 +36,7 @@ public LocalFileSystemCacheProvider( this._cacheFilePath = Path.Combine(tempPath, cacheFileName); } - /// + /// public bool IsCacheValid() { var cacheInvalid = true; @@ -53,7 +53,7 @@ public bool IsCacheValid() return !cacheInvalid; } - /// + /// public async Task GetAsync() { if (!this.IsCacheValid()) @@ -65,7 +65,7 @@ public async Task GetAsync() return await reader.ReadToEndAsync().ConfigureAwait(false); } - /// + /// public async Task SetAsync(string data) { using var streamWriter = File.CreateText(this._cacheFilePath); diff --git a/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj b/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj index 13196fe..704331c 100644 --- a/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj +++ b/src/Nager.PublicSuffix/Nager.PublicSuffix.csproj @@ -36,6 +36,7 @@ + diff --git a/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs b/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs index d47f7a5..085508f 100644 --- a/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs +++ b/src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs @@ -1,9 +1,11 @@ using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using Nager.PublicSuffix.CacheProviders; using Nager.PublicSuffix.Exceptions; using Nager.PublicSuffix.Extensions; using Nager.PublicSuffix.Models; using Nager.PublicSuffix.RuleParsers; +using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -16,6 +18,7 @@ namespace Nager.PublicSuffix.RuleProviders public class WebRuleProvider : IRuleProvider { private readonly string _dataFileUrl; + private readonly ILogger _logger; private readonly ICacheProvider _cacheProvider; private readonly HttpClient _httpClient; private DomainDataStructure _domainDataStructure; @@ -30,15 +33,18 @@ public class WebRuleProvider : IRuleProvider /// Loads the public suffix definition file from the official website /// /// It is possible to overwrite the url via configuration parameters Nager:PublicSuffix:DataUrl + /// /// /// /// public WebRuleProvider( + ILogger logger, IConfiguration configuration, ICacheProvider cacheProvider, HttpClient httpClient ) { + this._logger = logger; this._cacheProvider = cacheProvider; this._httpClient = httpClient; @@ -60,12 +66,24 @@ public async Task BuildAsync( string ruleData; if (this._cacheProvider.IsCacheValid()) { + this._logger.LogInformation($"{nameof(BuildAsync)} - Use data from cache"); ruleData = await this._cacheProvider.GetAsync().ConfigureAwait(false); } else { - ruleData = await this.LoadFromUrlAsync(this._dataFileUrl, cancellationToken).ConfigureAwait(false); - await this._cacheProvider.SetAsync(ruleData).ConfigureAwait(false); + this._logger.LogInformation($"{nameof(BuildAsync)} - Start downloading data from url"); + + try + { + ruleData = await this.LoadFromUrlAsync(this._dataFileUrl, cancellationToken).ConfigureAwait(false); + await this._cacheProvider.SetAsync(ruleData).ConfigureAwait(false); + } + catch (Exception exception) + { + this._logger.LogError(exception, $"{nameof(BuildAsync)} - Failure on download"); + + return false; + } } var rules = ruleParser.ParseRules(ruleData); @@ -79,7 +97,7 @@ public async Task BuildAsync( } /// - public DomainDataStructure GetDomainDataStructure() + public DomainDataStructure? GetDomainDataStructure() { return this._domainDataStructure; } @@ -95,7 +113,6 @@ public async Task LoadFromUrlAsync( CancellationToken cancellationToken) { using var response = await this._httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false); - if (!response.IsSuccessStatusCode) { throw new RuleLoadException($"Cannot load from {url} {response.StatusCode}");