From a313ce949202d3cc6263b8ff7136a34cf99715de Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Fri, 25 Sep 2020 19:56:46 -0500 Subject: [PATCH] =?UTF-8?q?fix(logging):=20=F0=9F=90=9B=20try=20yet=20anot?= =?UTF-8?q?her=20AppInsights=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HostRunner.cs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs b/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs index 1f5eec3a5..c91a2a0ad 100644 --- a/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs +++ b/services/SharedKernel/FilterLists.SharedKernel.Logging/HostRunner.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; @@ -13,13 +14,7 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func? run { _ = host ?? throw new ArgumentNullException(nameof(host)); - Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration - .WriteTo.Conditional( - _ => host.Services.GetService().IsProduction(), - c => c.ApplicationInsights( - TelemetryConfiguration.CreateDefault(), - TelemetryConverter.Traces)) - .CreateLogger(); + InitializeLogger(host); try { @@ -42,5 +37,23 @@ public static async Task TryRunWithLoggingAsync(this IHost host, Func? run Log.CloseAndFlush(); } } + + private static void InitializeLogger(IHost host) + { + var hostEnvironment = host.Services.GetRequiredService(); + Log.Logger = ConfigurationBuilder.BaseLoggerConfiguration + .ReadFrom.Configuration(host.Services.GetRequiredService()) + .Enrich.WithProperty("Application", hostEnvironment.ApplicationName) + .Enrich.WithProperty("Environment", hostEnvironment.EnvironmentName) + .WriteTo.Conditional( + _ => !hostEnvironment.IsProduction(), + sc => sc.Console().WriteTo.Debug()) + .WriteTo.Conditional( + _ => hostEnvironment.IsProduction(), + sc => sc.ApplicationInsights( + host.Services.GetRequiredService(), + TelemetryConverter.Traces)) + .CreateLogger(); + } } }