mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
inject shared logger into snap service
This commit is contained in:
parent
34756946e0
commit
e923fcc5b3
3 changed files with 12 additions and 24 deletions
|
|
@ -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<string> 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<SnapshotRule>()};
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<List<TSnap>> CreateAndSaveSnaps<TSnap>(IEnumerable<FilterList
|
|||
|
||||
private IEnumerable<TSnap> CreateSnaps<TSnap>(IEnumerable<FilterListViewUrlDto> 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<Snapshot> snaps)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue