use serilog AppInsights sink

This commit is contained in:
Collin M. Barrett 2019-07-12 09:46:21 -05:00
parent 6ffb388846
commit 5da473dc62
3 changed files with 27 additions and 6 deletions

View file

@ -43,13 +43,13 @@
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.10.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.11" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="Polly" Version="7.1.0" />
<PackageReference Include="RestSharp" Version="106.6.10" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.0.3" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="SharpCompress" Version="0.23.0" />
</ItemGroup>

View file

@ -9,9 +9,9 @@
using FilterLists.Agent.Infrastructure.GitHub;
using LibGit2Sharp;
using MediatR;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
using Serilog;
@ -59,12 +59,21 @@ private static void ConfigureCustom<TSettings>(this IServiceCollection services,
private static void AddLoggingCustom(this IServiceCollection services)
{
services.AddSingleton(b =>
{
var applicationInsightsSettings = b.GetService<IOptions<ApplicationInsightsSettings>>().Value;
return new TelemetryClient
{
InstrumentationKey = applicationInsightsSettings.InstrumentationKey
};
});
services.AddLogging(b =>
{
b.AddSerilog(new LoggerConfiguration().WriteTo.Console().CreateLogger());
var applicationInsightsSettings = b.Services.BuildServiceProvider()
.GetService<IOptions<ApplicationInsightsSettings>>().Value;
b.AddApplicationInsights(applicationInsightsSettings.InstrumentationKey);
var telemetryClient = b.Services.BuildServiceProvider().GetService<TelemetryClient>();
b.AddSerilog(new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.ApplicationInsights(telemetryClient, TelemetryConverter.Traces)
.CreateLogger());
});
}

View file

@ -1,10 +1,12 @@
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.DependencyInjection;
using MediatR;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.DependencyInjection;
namespace FilterLists.Agent
@ -18,6 +20,7 @@ public static async Task Main(string[] args)
BuildServiceProvider();
var parser = _serviceProvider.GetService<Parser>();
var mediator = _serviceProvider.GetService<IMediator>();
await parser.ParseArguments<CommandLineOptions>(args).MapResult(async o =>
{
if (o.ArchiveLists)
@ -27,6 +30,8 @@ await parser.ParseArguments<CommandLineOptions>(args).MapResult(async o =>
},
e => Task.FromResult(0)
);
FlushApplicationInsights();
}
private static void BuildServiceProvider()
@ -35,5 +40,12 @@ private static void BuildServiceProvider()
serviceCollection.RegisterAgentServices();
_serviceProvider = serviceCollection.BuildServiceProvider();
}
private static void FlushApplicationInsights()
{
var telemetryClient = _serviceProvider.GetService<TelemetryClient>();
telemetryClient.Flush();
Thread.Sleep(5000);
}
}
}