mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(archival): ✨ flush logs to AppInsights post command execution
This commit is contained in:
parent
0e2d650b51
commit
6a79430e7b
3 changed files with 54 additions and 2 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using FilterLists.Archival.Infrastructure.Clients;
|
||||
using FilterLists.Archival.Infrastructure.Logging;
|
||||
using FilterLists.Archival.Infrastructure.Persistence;
|
||||
using FilterLists.Archival.Infrastructure.Scheduling;
|
||||
using FilterLists.SharedKernel.Apis.Clients;
|
||||
|
|
@ -22,7 +23,7 @@ public static void AddInfrastructureServices(this IServiceCollection services, I
|
|||
{
|
||||
_ = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
||||
|
||||
services.AddSharedKernelLogging(configuration);
|
||||
services.AddArchivalLogging(configuration);
|
||||
services.AddSchedulingServices(configuration);
|
||||
services.AddApiClients(configuration);
|
||||
services.AddClients();
|
||||
|
|
@ -31,7 +32,7 @@ public static void AddInfrastructureServices(this IServiceCollection services, I
|
|||
|
||||
public static void UseInfrastructure(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseLogging();
|
||||
app.UseArchivalLogging();
|
||||
app.UseScheduling();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
using FilterLists.SharedKernel.Logging;
|
||||
using MediatR.Pipeline;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FilterLists.Archival.Infrastructure.Logging
|
||||
{
|
||||
internal static class ConfigurationExtensions
|
||||
{
|
||||
public static void AddArchivalLogging(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddSharedKernelLogging(configuration);
|
||||
services.AddTransient(typeof(IRequestPostProcessor<,>), typeof(TelemetryClientFlushPostProcessor<,>));
|
||||
}
|
||||
|
||||
public static void UseArchivalLogging(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseLogging();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediatR.Pipeline;
|
||||
using Microsoft.ApplicationInsights;
|
||||
|
||||
namespace FilterLists.Archival.Infrastructure.Logging
|
||||
{
|
||||
/// <remarks>https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#flushing-data</remarks>
|
||||
public class TelemetryClientFlushPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
{
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
|
||||
public TelemetryClientFlushPostProcessor(TelemetryClient telemetryClient)
|
||||
{
|
||||
_telemetryClient = telemetryClient;
|
||||
}
|
||||
|
||||
public Task Process(TRequest request, TResponse response, CancellationToken cancellationToken)
|
||||
{
|
||||
_telemetryClient.Flush();
|
||||
|
||||
// Allow some time for flushing before shutdown.
|
||||
Thread.Sleep(5000);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue