From 8126a489ac601c8b7a9e2424120955e42fd84f14 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 26 Sep 2020 08:44:42 -0500 Subject: [PATCH] =?UTF-8?q?fix(logging):=20=F0=9F=90=9B=20rollback=20loggi?= =?UTF-8?q?ng=20to=20previous=20working=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigurationBuilder.cs | 10 +++++++++- .../ConfigurationExtensions.cs | 14 +++++++------- .../FilterLists.SharedKernel.Logging.csproj | 3 +-- .../FilterLists.SharedKernel.Logging/HostRunner.cs | 13 +++++-------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs index ee13d408c..386902945 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs @@ -1,4 +1,5 @@ -using Serilog; +using Microsoft.ApplicationInsights; +using Serilog; using Serilog.Events; namespace FilterLists.SharedKernel.Logging @@ -12,5 +13,12 @@ internal static class ConfigurationBuilder .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console(); + + public static LoggerConfiguration WriteToApplicationInsights( + this LoggerConfiguration loggerConfiguration, + TelemetryClient telemetryClient) + { + return loggerConfiguration.WriteTo.ApplicationInsights(telemetryClient, TelemetryConverter.Traces); + } } } diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationExtensions.cs b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationExtensions.cs index b39ff1ee4..76ab48a6e 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationExtensions.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationExtensions.cs @@ -21,13 +21,13 @@ public static void AddSharedKernelLogging(this IServiceCollection services, ICon { _ = configuration ?? throw new ArgumentNullException(nameof(configuration)); - //using var serverTelemetryChannel = new ServerTelemetryChannel - //{ - // StorageFolder = configuration.GetSection(ApplicationInsightsOptions.Key) - // .Get() - // .ServerTelemetryChannelStoragePath - //}; - //services.AddSingleton(typeof(ITelemetryChannel), serverTelemetryChannel); + using var serverTelemetryChannel = new ServerTelemetryChannel + { + StorageFolder = configuration.GetSection(ApplicationInsightsOptions.Key) + .Get() + .ServerTelemetryChannelStoragePath + }; + services.AddSingleton(typeof(ITelemetryChannel), serverTelemetryChannel); services.AddApplicationInsightsTelemetry(); } diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj b/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj index a1480e1da..2bb27adb3 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj @@ -17,12 +17,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs b/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs index d5f30cecf..ebf0818bb 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; -using Microsoft.ApplicationInsights.Extensibility; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.ApplicationInsights; using Microsoft.Extensions.Hosting; using Serilog; @@ -9,16 +8,13 @@ namespace FilterLists.SharedKernel.Logging { public static class HostRunner { - public static async Task TryRunWithLoggingAsync(this IHost host, Func? runPreHostAsync = default) + public static async Task TryRunWithLoggingAsync(this IHost host, Func? runPreHostAsync = default) { _ = host ?? throw new ArgumentNullException(nameof(host)); + var telemetryClient = (TelemetryClient)host.Services.GetService(typeof(TelemetryClient)); Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration - .WriteTo.Conditional( - _ => host.Services.GetService().IsProduction(), - c => c.ApplicationInsights( - host.Services.GetRequiredService(), - TelemetryConverter.Traces)) + .WriteToApplicationInsights(telemetryClient) .CreateLogger(); try @@ -31,6 +27,7 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func? run Log.Information("Initializing host"); await host.RunAsync(); + return 0; } catch (Exception ex) {