add AI exception tracking to SnapshotService

This commit is contained in:
Collin M. Barrett 2018-08-20 13:56:50 -05:00
parent d566db9e59
commit 9b232ff278

View file

@ -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<IEnumerable<string>> 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);
}
}
}