mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
Revert "fix(logging): 🐛 try fix AppInisghts using inline initialization"
This reverts commit e31dc775a7.
This commit is contained in:
parent
e31dc775a7
commit
355ebeb008
4 changed files with 58 additions and 13 deletions
|
|
@ -1,7 +1,8 @@
|
|||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Archival.Application;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using static FilterLists.SharedKernel.Logging.HostRunner;
|
||||
|
||||
namespace FilterLists.Archival.Api
|
||||
{
|
||||
|
|
@ -10,7 +11,7 @@ public static class Program
|
|||
public static async Task Main(string[] args)
|
||||
{
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
await host.RunAsync();
|
||||
await host.TryRunWithLoggingAsync();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Directory.Application;
|
||||
using FilterLists.Directory.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using static FilterLists.SharedKernel.Logging.HostRunner;
|
||||
|
||||
namespace FilterLists.Directory.Api
|
||||
{
|
||||
|
|
@ -11,8 +12,7 @@ public static class Program
|
|||
public static async Task Main(string[] args)
|
||||
{
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
await host.MigrateAsync();
|
||||
await host.RunAsync();
|
||||
await host.TryRunWithLoggingAsync(async () => await host.MigrateAsync());
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using FilterLists.SharedKernel.Logging.Options;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -15,13 +14,7 @@ public static class ConfigurationExtensions
|
|||
{
|
||||
public static IHostBuilder UseLogging(this IHostBuilder hostBuilder)
|
||||
{
|
||||
return hostBuilder.UseSerilog((_, services, __) => ConfigurationBuilder.BaseLoggerConfiguration
|
||||
.WriteTo.Conditional(
|
||||
___ => services.GetService<IHostEnvironment>().IsProduction(),
|
||||
c => c.ApplicationInsights(
|
||||
services.GetRequiredService<TelemetryConfiguration>(),
|
||||
TelemetryConverter.Traces))
|
||||
.WriteTo.Console());
|
||||
return hostBuilder.UseSerilog();
|
||||
}
|
||||
|
||||
public static void AddSharedKernelLogging(this IServiceCollection services, IConfiguration configuration)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
|
||||
namespace FilterLists.SharedKernel.Logging
|
||||
{
|
||||
public static class HostRunner
|
||||
{
|
||||
public static async Task TryRunWithLoggingAsync(this IHost host, Func<Task>? runPreHostAsync = default)
|
||||
{
|
||||
_ = host ?? throw new ArgumentNullException(nameof(host));
|
||||
|
||||
Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration
|
||||
.WriteTo.Conditional(
|
||||
_ => host.Services.GetService<IHostEnvironment>().IsProduction(),
|
||||
c => c.ApplicationInsights(
|
||||
host.Services.GetRequiredService<TelemetryConfiguration>(),
|
||||
TelemetryConverter.Traces))
|
||||
.CreateLogger();
|
||||
|
||||
try
|
||||
{
|
||||
// TODO: rm, for debugging
|
||||
var client = host.Services.GetService<TelemetryClient>();
|
||||
Log.Warning("Application Insights Instrumentation Key: {InstrumentationKey}", client.InstrumentationKey);
|
||||
|
||||
if (runPreHostAsync != null)
|
||||
{
|
||||
Log.Information("Initializing pre-host");
|
||||
await runPreHostAsync();
|
||||
}
|
||||
|
||||
Log.Information("Initializing host");
|
||||
await host.RunAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Host terminated unexpectedly");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue