From 04b268eba00be83a85ae3847212384ff7c848923 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Thu, 24 Sep 2020 18:51:53 -0500 Subject: [PATCH] =?UTF-8?q?fix(logging):=20=F0=9F=90=9B=20try=20fix=20logg?= =?UTF-8?q?ing=20via=20alt=20AppInishgts=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigurationBuilder.cs | 3 +-- .../FilterLists.SharedKernel.Logging.csproj | 1 + .../FilterLists.SharedKernel.Logging/HostRunner.cs | 10 +++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs index 201781f6e..ee13d408c 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/ConfigurationBuilder.cs @@ -11,7 +11,6 @@ internal static class ConfigurationBuilder .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .Enrich.FromLogContext() - .WriteTo.Console() - .WriteTo.ApplicationInsights(TelemetryConverter.Traces); + .WriteTo.Console(); } } diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj b/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj index f3a829a24..06174563d 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/FilterLists.SharedKernel.Logging.csproj @@ -22,6 +22,7 @@ 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 552cd028a..d5f30cecf 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; @@ -11,7 +13,13 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func? run { _ = host ?? throw new ArgumentNullException(nameof(host)); - Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration.CreateLogger(); + Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration + .WriteTo.Conditional( + _ => host.Services.GetService().IsProduction(), + c => c.ApplicationInsights( + host.Services.GetRequiredService(), + TelemetryConverter.Traces)) + .CreateLogger(); try {