Skip to content

Commit

Permalink
Configure logging with appsettings.json
Browse files Browse the repository at this point in the history
Use appsettings.json to configure logging instead of code.
  • Loading branch information
martincostello committed Nov 26, 2023
1 parent 1b36e56 commit b976035
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
29 changes: 1 addition & 28 deletions src/LondonTravel.Skill/AlexaFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace MartinCostello.LondonTravel.Skill;

Expand Down Expand Up @@ -103,12 +102,7 @@ public Task<bool> InitializeAsync()
/// <param name="services">The service collection to configure.</param>
protected virtual void ConfigureServices(IServiceCollection services)
{
services.AddLogging((builder) =>
{
builder.SetMinimumLevel(LogLevel.Information)
.AddFilter(FilterLogs)
.AddJsonConsole();
});
services.AddLogging((builder) => builder.AddJsonConsole());

services.AddHttpClients();

Expand Down Expand Up @@ -140,27 +134,6 @@ protected virtual void Dispose(bool disposing)
}
}

/// <summary>
/// Filters the Lambda logs.
/// </summary>
/// <param name="name">The name of the log.</param>
/// <param name="level">The log level.</param>
/// <returns>
/// <see langword="true"/> to log the message; otherwise <see langword="false"/>.
/// </returns>
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;
}

/// <summary>
/// Creates the <see cref="ServiceProvider"/> to use.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/LondonTravel.Skill/LondonTravel.Skill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<Compile Update="Strings.Designer.cs" AutoGen="True" DependentUpon="Strings.resx" DesignTime="True" />
<EmbeddedResource Update="Strings.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Strings.Designer.cs" />
<EmbeddedResource Update="Strings.*.resx" DependentUpon="Strings.resx" />
<None Include="appsettings.json" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/LondonTravel.Skill/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Polly": "Warning",
"System": "Warning"
}
}
}

0 comments on commit b976035

Please sign in to comment.