From 2ac07a35c6db1f19f22092db0d1701a197b98d12 Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 26 Nov 2023 14:18:05 +0000 Subject: [PATCH] Configure logging with appsettings.json Use appsettings.json to configure logging instead of code. --- Directory.Packages.props | 3 + src/LondonTravel.Skill/AlexaFunction.cs | 55 ++++++++----------- .../LondonTravel.Skill.csproj | 4 ++ src/LondonTravel.Skill/SkillConfiguration.cs | 27 --------- src/LondonTravel.Skill/appsettings.json | 18 ++++++ .../LondonTravel.Skill.Tests/FunctionTests.cs | 30 ++++------ .../LondonTravel.Skill.Tests.csproj | 2 +- 7 files changed, 60 insertions(+), 79 deletions(-) create mode 100644 src/LondonTravel.Skill/appsettings.json diff --git a/Directory.Packages.props b/Directory.Packages.props index 3b7ba907..3c38dcbd 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -16,6 +16,9 @@ + + + diff --git a/src/LondonTravel.Skill/AlexaFunction.cs b/src/LondonTravel.Skill/AlexaFunction.cs index b495a8f1..8164bfce 100644 --- a/src/LondonTravel.Skill/AlexaFunction.cs +++ b/src/LondonTravel.Skill/AlexaFunction.cs @@ -7,10 +7,10 @@ using MartinCostello.LondonTravel.Skill.Intents; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; -using LogLevel = Microsoft.Extensions.Logging.LogLevel; +using Microsoft.Extensions.Options; namespace MartinCostello.LondonTravel.Skill; @@ -96,28 +96,42 @@ public Task InitializeAsync() return Task.FromResult(true); } + /// + /// Configures the to use. + /// + /// The configuration builder to configure. + protected virtual void Configure(ConfigurationBuilder builder) + { + builder.AddJsonFile("appsettings.json", optional: true) + .AddEnvironmentVariables(); + } + /// /// Configures the to use. /// /// The service collection to configure. protected virtual void ConfigureServices(IServiceCollection services) { - services.AddLogging((builder) => - { - builder.SetMinimumLevel(LogLevel.Information) - .AddFilter(FilterLogs) - .AddJsonConsole(); - }); + var builder = new ConfigurationBuilder(); - services.AddHttpClients(); + Configure(builder); + + var configuration = builder.Build(); + + services.AddOptions(); + services.Configure(configuration.GetSection("Skill")); - services.TryAddSingleton((_) => SkillConfiguration.CreateDefaultConfiguration()); + services.AddLogging((builder) => builder.AddJsonConsole()); + + services.AddHttpClients(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton((_) => TelemetryConfiguration.CreateDefault()); services.AddSingleton(CreateTelemetryClient); + services.AddSingleton((p) => p.GetRequiredService>().Value); + services.AddSingleton(configuration); services.AddSingleton(); services.AddSingleton(); @@ -162,27 +176,6 @@ private static TelemetryClient CreateTelemetryClient(IServiceProvider servicePro return new TelemetryClient(configuration); } - /// - /// Filters the Lambda logs. - /// - /// The name of the log. - /// The log level. - /// - /// to log the message; otherwise . - /// - private static bool FilterLogs(string name, LogLevel level) - { - if (level < LogLevel.Warning && - (name.StartsWith("System.", StringComparison.Ordinal) || - name.StartsWith("Microsoft.", StringComparison.Ordinal) || - name.StartsWith("Polly", StringComparison.Ordinal))) - { - return false; - } - - return true; - } - /// /// Creates the to use. /// diff --git a/src/LondonTravel.Skill/LondonTravel.Skill.csproj b/src/LondonTravel.Skill/LondonTravel.Skill.csproj index 87e78e2f..c9c17550 100644 --- a/src/LondonTravel.Skill/LondonTravel.Skill.csproj +++ b/src/LondonTravel.Skill/LondonTravel.Skill.csproj @@ -15,6 +15,9 @@ + + + @@ -29,6 +32,7 @@ + diff --git a/src/LondonTravel.Skill/SkillConfiguration.cs b/src/LondonTravel.Skill/SkillConfiguration.cs index 99cdc256..a1087e0f 100644 --- a/src/LondonTravel.Skill/SkillConfiguration.cs +++ b/src/LondonTravel.Skill/SkillConfiguration.cs @@ -37,31 +37,4 @@ public sealed class SkillConfiguration /// Gets or sets a value indicating whether to validate the skill's Id. /// public bool VerifySkillId { get; set; } - - /// - /// Create a default instance of the skill configuration. - /// - /// - /// The default to use. - /// - public static SkillConfiguration CreateDefaultConfiguration() - { - if (!bool.TryParse(GetEnvironmentVariable("VERIFY_SKILL_ID"), out bool verifySkillId)) - { - verifySkillId = false; - } - - return new SkillConfiguration() - { - ApplicationInsightsConnectionString = GetEnvironmentVariable("APPINSIGHTS_CONNECTIONSTRING"), - SkillApiUrl = GetEnvironmentVariable("SKILL_API_HOSTNAME"), - SkillId = GetEnvironmentVariable("SKILL_ID"), - TflApplicationId = GetEnvironmentVariable("TFL_APP_ID"), - TflApplicationKey = GetEnvironmentVariable("TFL_APP_KEY"), - VerifySkillId = verifySkillId, - }; - } - - private static string GetEnvironmentVariable(string name) - => Environment.GetEnvironmentVariable(name) ?? string.Empty; } diff --git a/src/LondonTravel.Skill/appsettings.json b/src/LondonTravel.Skill/appsettings.json new file mode 100644 index 00000000..80a9e532 --- /dev/null +++ b/src/LondonTravel.Skill/appsettings.json @@ -0,0 +1,18 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Polly": "Warning", + "System": "Warning" + } + }, + "Skill": { + "ApplicationInsightsConnectionString": "", + "SkillApiUrl": "", + "SkillId": "", + "TflApplicationId": "", + "TflApplicationKey": "", + "VerifySkillId": false + } +} diff --git a/test/LondonTravel.Skill.Tests/FunctionTests.cs b/test/LondonTravel.Skill.Tests/FunctionTests.cs index e11d2b56..8b7e96f9 100644 --- a/test/LondonTravel.Skill.Tests/FunctionTests.cs +++ b/test/LondonTravel.Skill.Tests/FunctionTests.cs @@ -6,6 +6,7 @@ using Alexa.NET.Response; using JustEat.HttpClientInterception; using MartinCostello.Logging.XUnit; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Http; using Microsoft.Extensions.Logging; @@ -36,26 +37,11 @@ protected virtual ResponseBody AssertResponse(SkillResponse actual, bool? should return actual.Response; } - protected virtual SkillConfiguration CreateConfiguration() - { - var config = SkillConfiguration.CreateDefaultConfiguration(); - - config.ApplicationInsightsConnectionString = "InstrumentationKey=my-application-insights-key;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/"; - config.SkillApiUrl = "https://londontravel.martincostello.local/"; - config.SkillId = "my-skill-id"; - config.TflApplicationId = "my-tfl-app-id"; - config.TflApplicationKey = "my-tfl-app-key"; - config.VerifySkillId = true; - - return config; - } - protected virtual async Task CreateFunctionAsync() { - SkillConfiguration config = CreateConfiguration(); - var function = new TestAlexaFunction(config, Interceptor, OutputHelper); + var function = new TestAlexaFunction(Interceptor, OutputHelper); - await function.InitializeAsync(); + _ = await function.InitializeAsync(); return function; } @@ -134,16 +120,20 @@ protected virtual SkillRequest CreateRequest(T request = null) } private sealed class TestAlexaFunction( - SkillConfiguration config, HttpClientInterceptorOptions options, ITestOutputHelper outputHelper) : AlexaFunction, ITestOutputHelperAccessor { public ITestOutputHelper OutputHelper { get; set; } = outputHelper; + protected override void Configure(ConfigurationBuilder builder) + { + base.Configure(builder); + builder.AddJsonFile("testsettings.json"); + } + protected override void ConfigureServices(IServiceCollection services) { - services.AddLogging((builder) => builder.AddXUnit(this).SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug)); - services.AddSingleton(config); + services.AddLogging((builder) => builder.AddXUnit(this)); services.AddSingleton( (_) => new HttpRequestInterceptionFilter(options)); diff --git a/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj b/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj index 460b18ad..a19ecbf4 100644 --- a/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj +++ b/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj @@ -21,7 +21,7 @@ - + true