mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor Agent telemetry
This commit is contained in:
parent
5d14763a0c
commit
04efb2abca
2 changed files with 49 additions and 31 deletions
30
src/FilterLists.Agent/Logger.cs
Normal file
30
src/FilterLists.Agent/Logger.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
|
||||
namespace FilterLists.Agent
|
||||
{
|
||||
public class Logger : IDisposable
|
||||
{
|
||||
private readonly TelemetryClient telemetryClient;
|
||||
|
||||
public Logger(string appInsightsKey)
|
||||
{
|
||||
TelemetryConfiguration.Active.InstrumentationKey = appInsightsKey;
|
||||
telemetryClient = new TelemetryClient();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
telemetryClient.Flush();
|
||||
Thread.Sleep(5000);
|
||||
}
|
||||
|
||||
public void Log(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
telemetryClient.TrackTrace(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using FilterLists.Services.DependencyInjection.Extensions;
|
||||
using FilterLists.Services.Snapshot;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
|
@ -12,51 +9,42 @@ namespace FilterLists.Agent
|
|||
public static class Program
|
||||
{
|
||||
private const int BatchSize = 1;
|
||||
private const string AiConfigSetting = "ApplicationInsights:InstrumentationKey";
|
||||
private static IConfigurationRoot configurationRoot;
|
||||
private const string AppInsightsKeyConfig = "ApplicationInsights:InstrumentationKey";
|
||||
private static IConfigurationRoot configRoot;
|
||||
private static ServiceProvider serviceProvider;
|
||||
private static TelemetryClient telemetryClient;
|
||||
private static Logger logger;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
BuildConfigurationRoot();
|
||||
InstantiateTelemetryClient();
|
||||
BuildConfigRoot();
|
||||
BuildServiceProvider();
|
||||
CaptureSnapshots(BatchSize);
|
||||
using (BuildLogger())
|
||||
{
|
||||
CaptureSnapshots(BatchSize);
|
||||
}
|
||||
}
|
||||
|
||||
private static void BuildConfigurationRoot() =>
|
||||
configurationRoot = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", false)
|
||||
.Build();
|
||||
|
||||
private static void InstantiateTelemetryClient()
|
||||
{
|
||||
TelemetryConfiguration.Active.InstrumentationKey = configurationRoot[AiConfigSetting];
|
||||
telemetryClient = new TelemetryClient();
|
||||
}
|
||||
private static void BuildConfigRoot() =>
|
||||
configRoot = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", false)
|
||||
.Build();
|
||||
|
||||
private static void BuildServiceProvider()
|
||||
{
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddFilterListsAgentServices(configurationRoot);
|
||||
serviceCollection.AddFilterListsAgentServices(configRoot);
|
||||
serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private static Logger BuildLogger() => logger = new Logger(configRoot[AppInsightsKeyConfig]);
|
||||
|
||||
private static void CaptureSnapshots(int batchSize)
|
||||
{
|
||||
var snapshotService = serviceProvider.GetService<SnapshotService>();
|
||||
Log("Capturing FilterList snapshots...");
|
||||
logger.Log("Capturing FilterList snapshots...");
|
||||
snapshotService.CaptureAsync(batchSize).Wait();
|
||||
Log("Snapshots captured.");
|
||||
telemetryClient.Flush();
|
||||
}
|
||||
|
||||
private static void Log(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
telemetryClient.TrackTrace(message);
|
||||
logger.Log("Snapshots captured.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue