From e923fcc5b3fb08dfb2d23d883c72e75f8d900efb Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Sat, 1 Sep 2018 15:59:39 -0500 Subject: [PATCH] inject shared logger into snap service --- src/FilterLists.Services/Snapshot/Snapshot.cs | 23 +++++-------------- .../Snapshot/SnapshotService.cs | 9 ++++---- .../Snapshot/SnapshotWayback.cs | 4 ++-- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/FilterLists.Services/Snapshot/Snapshot.cs b/src/FilterLists.Services/Snapshot/Snapshot.cs index 80c441908..163c853e4 100644 --- a/src/FilterLists.Services/Snapshot/Snapshot.cs +++ b/src/FilterLists.Services/Snapshot/Snapshot.cs @@ -5,14 +5,12 @@ using System.Net; using System.Net.Http; using System.Security.Cryptography; -using System.Threading; using System.Threading.Tasks; using FilterLists.Data; using FilterLists.Data.Entities.Junctions; using FilterLists.Services.Extensions; using FilterLists.Services.Snapshot.Models; using JetBrains.Annotations; -using Microsoft.ApplicationInsights; using Microsoft.EntityFrameworkCore; using MoreLinq; using SharpCompress.Archives.SevenZip; @@ -25,7 +23,7 @@ public class Snapshot public readonly FilterListViewUrlDto List; protected readonly Data.Entities.Snapshot SnapEntity; private readonly FilterListsDbContext dbContext; - private readonly TelemetryClient telemetryClient; + private readonly Logger logger; private readonly string uaString; protected string ListUrl; private HashSet lines; @@ -35,14 +33,14 @@ public Snapshot() } [UsedImplicitly] - public Snapshot(FilterListsDbContext dbContext, FilterListViewUrlDto list, string uaString) + public Snapshot(FilterListsDbContext dbContext, FilterListViewUrlDto list, Logger logger, string uaString) { this.dbContext = dbContext; List = list; ListUrl = list.ViewUrl; + this.logger = logger; SnapEntity = new Data.Entities.Snapshot {FilterListId = list.Id, SnapshotRules = new List()}; this.uaString = uaString; - telemetryClient = new TelemetryClient(); } public bool WebExcepted { get; private set; } @@ -60,7 +58,7 @@ protected async Task TrySaveAsyncBase() } catch (Exception e) { - TrackException(e); + logger.Log(e); } } @@ -90,14 +88,14 @@ private async Task TryGetLines() { WebExcepted = true; await dbContext.SaveChangesAsync(); - TrackException(hre); + logger.Log(hre); } catch (WebException we) { WebExcepted = true; SnapEntity.HttpStatusCode = (int)((HttpWebResponse)we.Response).StatusCode; await dbContext.SaveChangesAsync(); - TrackException(we); + logger.Log(we); } } @@ -195,14 +193,5 @@ private async Task SetSuccessful() SnapEntity.WasSuccessful = true; await dbContext.SaveChangesAsync(); } - - private void TrackException(Exception e) - { - telemetryClient.TrackException(e); - telemetryClient.Flush(); - - //https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data - Thread.Sleep(5000); - } } } \ No newline at end of file diff --git a/src/FilterLists.Services/Snapshot/SnapshotService.cs b/src/FilterLists.Services/Snapshot/SnapshotService.cs index 3ba75a1be..725b9969d 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotService.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotService.cs @@ -30,12 +30,11 @@ public class SnapshotService : Service .OrderByDescending(d => d) .FirstOrDefault(); + private readonly Logger logger; private string uaString; - public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig) - : base(dbContext, mapConfig) - { - } + public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig, Logger logger) + : base(dbContext, mapConfig) => this.logger = logger; public async Task CaptureAsync(int batchSize) { @@ -74,7 +73,7 @@ private async Task> CreateAndSaveSnaps(IEnumerable CreateSnaps(IEnumerable lists) where TSnap : Snapshot, new() => - lists.Select(l => Activator.CreateInstance(typeof(TSnap), DbContext, l, uaString) as TSnap); + lists.Select(l => Activator.CreateInstance(typeof(TSnap), DbContext, l, logger, uaString) as TSnap); private static async Task SaveSnaps(IEnumerable snaps) { diff --git a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs index c51d24750..2daab965e 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotWayback.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotWayback.cs @@ -13,8 +13,8 @@ public SnapshotWayback() } [UsedImplicitly] - public SnapshotWayback(FilterListsDbContext dbContext, FilterListViewUrlDto list, string uaString) - : base(dbContext, list, uaString) + public SnapshotWayback(FilterListsDbContext dbContext, FilterListViewUrlDto list, Logger logger, string uaString) + : base(dbContext, list, logger, uaString) { }