Skip to content

Commit a57970b

Browse files
Update default log level (#1970)
1 parent 05f6bc7 commit a57970b

File tree

10 files changed

+229
-5
lines changed

10 files changed

+229
-5
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.TestTool",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Update default log level to be ERROR in production and INFORMATION in debug mode."
8+
]
9+
}
10+
]
11+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Amazon.Lambda.TestTool.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@
5656
<ItemGroup>
5757
<EmbeddedResource Include="Resources\**" />
5858
</ItemGroup>
59+
<ItemGroup>
60+
<None Update="appsettings.json">
61+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
62+
</None>
63+
<None Update="appsettings.Development.json">
64+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
65+
</None>
66+
</ItemGroup>
67+
5968
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Reflection;
5+
6+
namespace Amazon.Lambda.TestTool.Configuration;
7+
8+
/// <summary>
9+
/// Handles the configuration setup for the Lambda Test Tool by loading settings from JSON files.
10+
/// </summary>
11+
/// <remarks>
12+
/// This class uses dependency injection to receive an Assembly instance, allowing for better testability
13+
/// and flexibility in determining the configuration file locations.
14+
/// </remarks>
15+
public class ConfigurationSetup(Assembly assembly)
16+
{
17+
/// <summary>
18+
/// Retrieves the application configuration by loading settings from JSON configuration files.
19+
/// </summary>
20+
/// <returns>An IConfiguration instance containing the application settings.</returns>
21+
/// <exception cref="InvalidOperationException">Thrown when unable to determine the assembly location.</exception>
22+
/// <remarks>
23+
/// The method performs the following steps:
24+
/// 1. Locates the directory containing the assembly
25+
/// 2. Loads the base configuration from appsettings.json
26+
/// 3. Loads environment-specific configuration from appsettings.{environment}.json if available
27+
/// </remarks>
28+
public IConfiguration GetConfiguration()
29+
{
30+
// Get the directory where the assembly is located
31+
var assemblyLocation = assembly.Location;
32+
var packageDirectory = Path.GetDirectoryName(assemblyLocation)
33+
?? throw new InvalidOperationException("Unable to determine assembly location");
34+
35+
// Construct path to configuration file
36+
var appsettingsPath = Path.Combine(packageDirectory, "appsettings.json");
37+
if (!File.Exists(appsettingsPath))
38+
{
39+
Console.WriteLine($"Warning: appsettings.json not found at {appsettingsPath}");
40+
}
41+
42+
// Determine the current environment
43+
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
44+
45+
// Build and return the configuration
46+
var builder = new ConfigurationBuilder()
47+
.SetBasePath(packageDirectory)
48+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
49+
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true);
50+
51+
return builder.Build();
52+
}
53+
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Reflection;
5+
using Amazon.Lambda.TestTool.Configuration;
46
using Amazon.Lambda.TestTool.Services;
57
using Amazon.Lambda.TestTool.Services.IO;
68
using Microsoft.Extensions.DependencyInjection.Extensions;

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/ApiGatewayEmulatorProcess.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Reflection;
45
using Amazon.Lambda.APIGatewayEvents;
56
using Amazon.Lambda.Model;
67
using Amazon.Lambda.TestTool.Commands.Settings;
@@ -9,6 +10,8 @@
910
using Amazon.Lambda.TestTool.Services;
1011

1112
using System.Text.Json;
13+
using Amazon.Lambda.TestTool.Configuration;
14+
using Amazon.Lambda.TestTool.Utilities;
1215

1316
namespace Amazon.Lambda.TestTool.Processes;
1417

@@ -46,6 +49,8 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
4649

4750
var builder = WebApplication.CreateBuilder();
4851

52+
Utils.ConfigureWebApplicationBuilder(builder);
53+
4954
builder.Services.AddApiGatewayEmulatorServices();
5055

5156
var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/TestToolProcess.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Reflection;
45
using Amazon.Lambda.TestTool.Commands.Settings;
56
using Amazon.Lambda.TestTool.Components;
7+
using Amazon.Lambda.TestTool.Configuration;
68
using Amazon.Lambda.TestTool.Services;
79
using Amazon.Lambda.TestTool.Services.IO;
10+
using Amazon.Lambda.TestTool.Utilities;
811
using Microsoft.Extensions.FileProviders;
912
using Microsoft.Extensions.Options;
1013

@@ -37,6 +40,8 @@ public static TestToolProcess Startup(RunCommandSettings settings, CancellationT
3740
{
3841
var builder = WebApplication.CreateBuilder();
3942

43+
Utils.ConfigureWebApplicationBuilder(builder);
44+
4045
builder.Services.AddSingleton<IRuntimeApiDataStoreManager, RuntimeApiDataStoreManager>();
4146
builder.Services.AddSingleton<IThemeService, ThemeService>();
4247

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Utilities/Utils.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Reflection;
55
using System.Text.Json;
6+
using Amazon.Lambda.TestTool.Configuration;
67

78
namespace Amazon.Lambda.TestTool.Utilities;
89

@@ -74,4 +75,32 @@ public static string TryPrettyPrintJson(string? data)
7475
return data ?? string.Empty;
7576
}
7677
}
78+
79+
/// <summary>
80+
/// Configures the web application builder with necessary services, configuration, and logging setup.
81+
/// </summary>
82+
/// <param name="builder">The WebApplicationBuilder instance to be configured</param>
83+
/// <remarks>
84+
/// This method performs the following configurations:
85+
/// 1. Registers the current assembly as a singleton service
86+
/// 2. Registers ConfigurationSetup as a singleton service
87+
/// 3. Builds and applies custom configuration
88+
/// 4. Sets up logging providers with console output
89+
/// </remarks>
90+
/// <exception cref="InvalidOperationException">
91+
/// Thrown when ConfigurationSetup service cannot be resolved from the service provider
92+
/// </exception>
93+
public static void ConfigureWebApplicationBuilder(WebApplicationBuilder builder)
94+
{
95+
builder.Services.AddSingleton(typeof(Assembly), typeof(ConfigurationSetup).Assembly);
96+
builder.Services.AddSingleton<ConfigurationSetup>();
97+
98+
var configSetup = builder.Services.BuildServiceProvider().GetRequiredService<ConfigurationSetup>();
99+
var configuration = configSetup.GetConfiguration();
100+
builder.Configuration.AddConfiguration(configuration);
101+
102+
builder.Logging.ClearProviders();
103+
builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
104+
builder.Logging.AddConsole();
105+
}
77106
}

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/appsettings.Development.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Trace",
5-
"Microsoft.AspNetCore": "Trace"
4+
"Default": "Information"
65
}
76
},
87
"DetailedErrors": true

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/appsettings.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Trace",
5-
"Microsoft": "Trace",
6-
"Microsoft.AspNetCore": "Trace"
4+
"Default": "Error"
75
}
86
},
97
"AllowedHosts": "*"
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System.Reflection;
5+
using Amazon.Lambda.TestTool.Configuration;
6+
using Moq;
7+
using Xunit;
8+
9+
namespace Amazon.Lambda.TestTool.UnitTests.Configuration;
10+
11+
public class ConfigurationSetupTests
12+
{
13+
[Fact]
14+
public void GetConfiguration_WithValidAssemblyLocation_ReturnsConfiguration()
15+
{
16+
// Arrange
17+
var mockAssembly = new Mock<Assembly>();
18+
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
19+
Directory.CreateDirectory(testDirectory);
20+
21+
try
22+
{
23+
// Create test appsettings.json
24+
File.WriteAllText(
25+
Path.Combine(testDirectory, "appsettings.json"),
26+
@"{""TestSetting"": ""TestValue""}"
27+
);
28+
29+
mockAssembly
30+
.Setup(x => x.Location)
31+
.Returns(Path.Combine(testDirectory, "dummy.dll"));
32+
33+
var configSetup = new ConfigurationSetup(mockAssembly.Object);
34+
35+
// Act
36+
var configuration = configSetup.GetConfiguration();
37+
38+
// Assert
39+
Assert.NotNull(configuration);
40+
Assert.Equal("TestValue", configuration["TestSetting"]);
41+
}
42+
finally
43+
{
44+
// Cleanup
45+
Directory.Delete(testDirectory, true);
46+
}
47+
}
48+
49+
[Fact]
50+
public void GetConfiguration_WithEnvironmentSpecificConfig_LoadsBothConfigs()
51+
{
52+
// Arrange
53+
var mockAssembly = new Mock<Assembly>();
54+
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
55+
Directory.CreateDirectory(testDirectory);
56+
57+
try
58+
{
59+
// Create test appsettings.json
60+
File.WriteAllText(
61+
Path.Combine(testDirectory, "appsettings.json"),
62+
@"{""TestSetting"": ""BaseValue"", ""CommonSetting"": ""BaseValue""}"
63+
);
64+
65+
// Create test appsettings.Development.json
66+
File.WriteAllText(
67+
Path.Combine(testDirectory, "appsettings.Development.json"),
68+
@"{""TestSetting"": ""DevValue""}"
69+
);
70+
71+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
72+
73+
mockAssembly
74+
.Setup(x => x.Location)
75+
.Returns(Path.Combine(testDirectory, "dummy.dll"));
76+
77+
var configSetup = new ConfigurationSetup(mockAssembly.Object);
78+
79+
// Act
80+
var configuration = configSetup.GetConfiguration();
81+
82+
// Assert
83+
Assert.NotNull(configuration);
84+
Assert.Equal("DevValue", configuration["TestSetting"]); // Overridden value
85+
Assert.Equal("BaseValue", configuration["CommonSetting"]); // Base value
86+
}
87+
finally
88+
{
89+
// Cleanup
90+
Directory.Delete(testDirectory, true);
91+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
92+
}
93+
}
94+
95+
[Fact]
96+
public void GetConfiguration_WithInvalidAssemblyLocation_ThrowsInvalidOperationException()
97+
{
98+
// Arrange
99+
var mockAssembly = new Mock<Assembly>();
100+
mockAssembly
101+
.Setup(x => x.Location)
102+
.Returns(string.Empty);
103+
104+
var configSetup = new ConfigurationSetup(mockAssembly.Object);
105+
106+
// Act & Assert
107+
var exception = Assert.Throws<InvalidOperationException>(
108+
() => configSetup.GetConfiguration()
109+
);
110+
Assert.Equal("Unable to determine assembly location", exception.Message);
111+
}
112+
}
113+

0 commit comments

Comments
 (0)