mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
add IDisposable wrapper for TelemetryClient to let DI handle flush and pause
https://github.com/Microsoft/ApplicationInsights-dotnet/issues/596#issuecomment-320371794
This commit is contained in:
parent
d6f6e559df
commit
0097ddd3e0
4 changed files with 40 additions and 31 deletions
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
|
||||
namespace FilterLists.Agent.Infrastructure.ApplicationInsights
|
||||
{
|
||||
public class AgentTelemetryClient : IDisposable
|
||||
{
|
||||
public AgentTelemetryClient(TelemetryConfiguration telemetryConfiguration)
|
||||
{
|
||||
TelemetryClient = new TelemetryClient(telemetryConfiguration);
|
||||
}
|
||||
|
||||
public TelemetryClient TelemetryClient { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposing)
|
||||
return;
|
||||
TelemetryClient.Flush();
|
||||
Thread.Sleep(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Agent.AppSettings;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
|
@ -21,7 +20,12 @@ public static void AddApplicationInsights(this IServiceCollection services)
|
|||
services.AddSingleton(b =>
|
||||
{
|
||||
var configuration = b.GetService<TelemetryConfiguration>();
|
||||
return new TelemetryClient(configuration);
|
||||
return new AgentTelemetryClient(configuration);
|
||||
});
|
||||
services.AddSingleton(b =>
|
||||
{
|
||||
var telemetryConfiguration = b.GetService<TelemetryConfiguration>();
|
||||
return QuickPulseTelemetryModuleBuilder.Build(telemetryConfiguration);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
using FilterLists.Agent.Infrastructure.Polly;
|
||||
using FilterLists.Agent.Infrastructure.Web;
|
||||
using MediatR;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
|
|
@ -59,7 +58,8 @@ private static void AddLogging(this IServiceCollection services)
|
|||
{
|
||||
services.AddLogging(b =>
|
||||
{
|
||||
var telemetryClient = b.Services.BuildServiceProvider().GetService<TelemetryClient>();
|
||||
var telemetryClient = b.Services.BuildServiceProvider().GetService<AgentTelemetryClient>()
|
||||
.TelemetryClient;
|
||||
b.AddSerilog(new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.ApplicationInsights(telemetryClient, TelemetryConverter.Traces)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using FilterLists.Agent.Features.Lists;
|
||||
using FilterLists.Agent.Features.Urls;
|
||||
using FilterLists.Agent.Infrastructure.ApplicationInsights;
|
||||
using FilterLists.Agent.Infrastructure.DependencyInjection;
|
||||
using MediatR;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FilterLists.Agent
|
||||
|
|
@ -17,12 +12,10 @@ namespace FilterLists.Agent
|
|||
public static class Program
|
||||
{
|
||||
private static IServiceProvider _serviceProvider;
|
||||
private static QuickPulseTelemetryModule _quickPulseTelemetryModule;
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Setup();
|
||||
|
||||
_serviceProvider = ServiceProviderBuilder.Build();
|
||||
var parser = _serviceProvider.GetService<Parser>();
|
||||
var mediator = _serviceProvider.GetService<IMediator>();
|
||||
await parser.ParseArguments<CommandLineOptions>(args).MapResult(async o =>
|
||||
|
|
@ -34,25 +27,6 @@ await parser.ParseArguments<CommandLineOptions>(args).MapResult(async o =>
|
|||
},
|
||||
e => Task.FromResult(0)
|
||||
);
|
||||
|
||||
Teardown();
|
||||
}
|
||||
|
||||
private static void Setup()
|
||||
{
|
||||
_serviceProvider = ServiceProviderBuilder.Build();
|
||||
|
||||
var telemetryConfiguration = _serviceProvider.GetService<TelemetryConfiguration>();
|
||||
_quickPulseTelemetryModule = QuickPulseTelemetryModuleBuilder.Build(telemetryConfiguration);
|
||||
}
|
||||
|
||||
private static void Teardown()
|
||||
{
|
||||
_quickPulseTelemetryModule.Dispose();
|
||||
|
||||
var telemetryClient = _serviceProvider.GetService<TelemetryClient>();
|
||||
telemetryClient.Flush();
|
||||
Thread.Sleep(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue