From ec2802a0a122d4ce660e7c827921944e3773833a Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 20 Aug 2018 13:56:50 -0500 Subject: [PATCH] add AI exception tracking to SnapshotService --- src/FilterLists.Services/Snapshot/Snapshot.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/FilterLists.Services/Snapshot/Snapshot.cs b/src/FilterLists.Services/Snapshot/Snapshot.cs index e2c8605a15..0e63d382d3 100644 --- a/src/FilterLists.Services/Snapshot/Snapshot.cs +++ b/src/FilterLists.Services/Snapshot/Snapshot.cs @@ -4,11 +4,13 @@ using System.Linq; using System.Net; using System.Net.Http; +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 Microsoft.ApplicationInsights; using MoreLinq; namespace FilterLists.Services.Snapshot @@ -19,11 +21,13 @@ public class Snapshot private readonly FilterListsDbContext dbContext; private readonly FilterListViewUrlDto list; private readonly Data.Entities.Snapshot snapEntity; + private readonly TelemetryClient telemetryClient; public Snapshot(FilterListsDbContext dbContext, FilterListViewUrlDto list) { this.dbContext = dbContext; this.list = list; + telemetryClient = new TelemetryClient(); snapEntity = new Data.Entities.Snapshot { FilterListId = list.Id, @@ -39,10 +43,9 @@ public async Task TrySaveAsync() { await SaveAsync(); } - catch (Exception) + catch (Exception e) { - //allow other snapshots to continue - //TODO: log + TrackException(e); } } @@ -74,13 +77,15 @@ private async Task> TryGetLines() { return await GetLines(); } - catch (HttpRequestException) + catch (HttpRequestException hre) { + TrackException(hre); return null; } catch (WebException we) { snapEntity.HttpStatusCode = ((int)((HttpWebResponse)we.Response).StatusCode).ToString(); + TrackException(we); return null; } } @@ -155,5 +160,12 @@ private async Task SetSuccessful() snapEntity.WasSuccessful = true; await dbContext.SaveChangesAsync(); } + + private void TrackException(Exception e) + { + telemetryClient.TrackException(e); + telemetryClient.Flush(); + Thread.Sleep(5000); + } } } \ No newline at end of file