From b484d2ee4f31b0242308fcf7ba1378767fe7a97f Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 6 Jul 2017 12:24:38 -0700 Subject: [PATCH] Remove default AI logger settings (#69) --- ...icationInsightsHostingStartupSample.csproj | 5 +++- .../Startup.cs | 11 ++++---- .../appsettings.json | 13 +++------- .../ApplicationInsightsLoggerStartupFilter.cs | 5 +--- .../ApplicationInsightsStartupLoader.cs | 25 ------------------- 5 files changed, 15 insertions(+), 44 deletions(-) diff --git a/sample/ApplicationInsightsHostingStartupSample/ApplicationInsightsHostingStartupSample.csproj b/sample/ApplicationInsightsHostingStartupSample/ApplicationInsightsHostingStartupSample.csproj index 2fadb39ae1..406899ac46 100644 --- a/sample/ApplicationInsightsHostingStartupSample/ApplicationInsightsHostingStartupSample.csproj +++ b/sample/ApplicationInsightsHostingStartupSample/ApplicationInsightsHostingStartupSample.csproj @@ -22,5 +22,8 @@ - + + + + diff --git a/sample/ApplicationInsightsHostingStartupSample/Startup.cs b/sample/ApplicationInsightsHostingStartupSample/Startup.cs index f34a884340..3b43a49cfa 100644 --- a/sample/ApplicationInsightsHostingStartupSample/Startup.cs +++ b/sample/ApplicationInsightsHostingStartupSample/Startup.cs @@ -73,20 +73,21 @@ namespace IISSample public static void Main(string[] args) { - var config = new ConfigurationBuilder() - .AddCommandLine(args) - .AddEnvironmentVariables(prefix: "ASPNETCORE_") - .Build(); var host = new WebHostBuilder() + .ConfigureAppConfiguration((hostingContext, config) => + { + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + config.AddCommandLine(args); + }) .ConfigureLogging((hostingContext, builder) => { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); builder.AddConsole(); }) .UseKestrel() .UseStartup() .UseContentRoot(Directory.GetCurrentDirectory()) - .UseConfiguration(config) .Build(); host.Run(); diff --git a/sample/ApplicationInsightsHostingStartupSample/appsettings.json b/sample/ApplicationInsightsHostingStartupSample/appsettings.json index a7581a6b98..f948239fb4 100644 --- a/sample/ApplicationInsightsHostingStartupSample/appsettings.json +++ b/sample/ApplicationInsightsHostingStartupSample/appsettings.json @@ -4,15 +4,10 @@ }, "Logging": { "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Warning" - } - }, - "Console": { - "LogLevel": { - "Default": "Warning" - } + "LogLevel": { + "Default": "Information", + "System": "Warning", + "Microsoft": "Warning" } } } diff --git a/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsLoggerStartupFilter.cs b/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsLoggerStartupFilter.cs index 47eb9a2549..6fb2a502a7 100644 --- a/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsLoggerStartupFilter.cs +++ b/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsLoggerStartupFilter.cs @@ -19,14 +19,11 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup // We need to disable filtering on logger, filtering would be done by LoggerFactory var loggerEnabled = true; - Action disableCallback = () => loggerEnabled = false; - // We detected that logger settings got to LoggerFactory configuration and - // defaults would be applied loggerFactory.AddApplicationInsights( builder.ApplicationServices, (s, level) => loggerEnabled, - disableCallback); + () => loggerEnabled = false); next(builder); }; diff --git a/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsStartupLoader.cs b/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsStartupLoader.cs index 6211ae3610..746f5f5dc3 100644 --- a/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsStartupLoader.cs +++ b/src/Microsoft.AspNetCore.ApplicationInsights.HostingStartup/ApplicationInsightsStartupLoader.cs @@ -45,7 +45,6 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup /// The associated with the application. private void InitializeServices(IServiceCollection services) { - services.AddSingleton, DefaultApplicationInsightsLoggerFilters>(); services.AddSingleton(); services.AddSingleton(); @@ -59,28 +58,4 @@ namespace Microsoft.AspNetCore.ApplicationInsights.HostingStartup } } } - - internal class DefaultApplicationInsightsLoggerFilters : IConfigureOptions - { - private const string ApplicationInsightsLoggerFactory = "Microsoft.ApplicationInsights.AspNetCore.Logging.ApplicationInsightsLoggerProvider"; - - private static readonly KeyValuePair[] _defaultLoggingLevels = { - new KeyValuePair("Microsoft", LogLevel.Warning), - new KeyValuePair("System", LogLevel.Warning), - new KeyValuePair(null, LogLevel.Information) - }; - - public void Configure(LoggerFilterOptions options) - { - foreach (var pair in _defaultLoggingLevels) - { - options.Rules.Add(new LoggerFilterRule( - ApplicationInsightsLoggerFactory, - pair.Key, - null, - (type, name, level) => Debugger.IsAttached || level >= pair.Value - )); - } - } - } }