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) {