Skip to content

Commit

Permalink
Use JSON logging
Browse files Browse the repository at this point in the history
- Use the .NET JSON console logger.
- Filter out logs from Polly.
  • Loading branch information
martincostello committed Nov 26, 2023
1 parent ea648ae commit 252f2e2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<ItemGroup>
<PackageVersion Include="Alexa.NET" Version="1.22.0" />
<PackageVersion Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageVersion Include="Amazon.Lambda.Logging.AspNetCore" Version="3.1.0" />
<PackageVersion Include="Amazon.Lambda.RuntimeSupport" Version="1.10.0" />
<PackageVersion Include="Amazon.Lambda.Serialization.Json" Version="2.1.1" />
<PackageVersion Include="Amazon.Lambda.TestUtilities" Version="2.0.0" />
Expand All @@ -20,7 +19,7 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.ICU.ICU4C.Runtime" Version="72.1.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
12 changes: 5 additions & 7 deletions src/LondonTravel.Skill/AlexaFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ protected virtual void ConfigureServices(IServiceCollection services)
{
services.AddLogging((builder) =>
{
var options = new LambdaLoggerOptions()
{
Filter = FilterLogs,
};

builder.AddLambdaLogger(options);
builder.SetMinimumLevel(LogLevel.Information)
.AddFilter(FilterLogs)
.AddJsonConsole();
});

services.AddHttpClients();
Expand Down Expand Up @@ -178,7 +175,8 @@ private static bool FilterLogs(string name, LogLevel level)
{
if (level < LogLevel.Warning &&
(name.StartsWith("System.", StringComparison.Ordinal) ||
name.StartsWith("Microsoft.", StringComparison.Ordinal)))
name.StartsWith("Microsoft.", StringComparison.Ordinal) ||
name.StartsWith("Polly", StringComparison.Ordinal)))
{
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/LondonTravel.Skill/LondonTravel.Skill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
<ItemGroup>
<PackageReference Include="Alexa.NET" />
<PackageReference Include="Amazon.Lambda.Core" />
<PackageReference Include="Amazon.Lambda.Logging.AspNetCore" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" />
<PackageReference Include="Microsoft.ApplicationInsights" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="Polly.Extensions" />
Expand Down
2 changes: 1 addition & 1 deletion test/LondonTravel.Skill.Tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private sealed class TestAlexaFunction(

protected override void ConfigureServices(IServiceCollection services)
{
services.AddLogging((builder) => builder.AddXUnit(this));
services.AddLogging((builder) => builder.AddXUnit(this).SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug));
services.AddSingleton(config);
services.AddSingleton<IHttpMessageHandlerBuilderFilter, HttpRequestInterceptionFilter>(
(_) => new HttpRequestInterceptionFilter(options));
Expand Down

0 comments on commit 252f2e2

Please sign in to comment.