Skip to content

Update default log level #1970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/1c4eb3ce-e61b-4119-ac8d-d431f7b426cb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.TestTool",
"Type": "Patch",
"ChangelogMessages": [
"Update default log level to be ERROR in production and INFORMATION in debug mode."
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@
<ItemGroup>
<EmbeddedResource Include="Resources\**" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;

namespace Amazon.Lambda.TestTool.Configuration;

/// <summary>
/// Handles the configuration setup for the Lambda Test Tool by loading settings from JSON files.
/// </summary>
/// <remarks>
/// This class uses dependency injection to receive an Assembly instance, allowing for better testability
/// and flexibility in determining the configuration file locations.
/// </remarks>
public class ConfigurationSetup(Assembly assembly)
{
/// <summary>
/// Retrieves the application configuration by loading settings from JSON configuration files.
/// </summary>
/// <returns>An IConfiguration instance containing the application settings.</returns>
/// <exception cref="InvalidOperationException">Thrown when unable to determine the assembly location.</exception>
/// <remarks>
/// The method performs the following steps:
/// 1. Locates the directory containing the assembly
/// 2. Loads the base configuration from appsettings.json
/// 3. Loads environment-specific configuration from appsettings.{environment}.json if available
/// </remarks>
public IConfiguration GetConfiguration()
{
// Get the directory where the assembly is located
var assemblyLocation = assembly.Location;
var packageDirectory = Path.GetDirectoryName(assemblyLocation)
?? throw new InvalidOperationException("Unable to determine assembly location");

// Construct path to configuration file
var appsettingsPath = Path.Combine(packageDirectory, "appsettings.json");
if (!File.Exists(appsettingsPath))
{
Console.WriteLine($"Warning: appsettings.json not found at {appsettingsPath}");
}

// Determine the current environment
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

// Build and return the configuration
var builder = new ConfigurationBuilder()
.SetBasePath(packageDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true);

return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;
using Amazon.Lambda.TestTool.Configuration;
using Amazon.Lambda.TestTool.Services;
using Amazon.Lambda.TestTool.Services.IO;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Model;
using Amazon.Lambda.TestTool.Commands.Settings;
Expand All @@ -9,6 +10,8 @@
using Amazon.Lambda.TestTool.Services;

using System.Text.Json;
using Amazon.Lambda.TestTool.Configuration;
using Amazon.Lambda.TestTool.Utilities;

namespace Amazon.Lambda.TestTool.Processes;

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

var builder = WebApplication.CreateBuilder();

Utils.ConfigureWebApplicationBuilder(builder);

builder.Services.AddApiGatewayEmulatorServices();

var serviceUrl = $"http://{settings.LambdaEmulatorHost}:{settings.ApiGatewayEmulatorPort}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;
using Amazon.Lambda.TestTool.Commands.Settings;
using Amazon.Lambda.TestTool.Components;
using Amazon.Lambda.TestTool.Configuration;
using Amazon.Lambda.TestTool.Services;
using Amazon.Lambda.TestTool.Services.IO;
using Amazon.Lambda.TestTool.Utilities;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;

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

Utils.ConfigureWebApplicationBuilder(builder);

builder.Services.AddSingleton<IRuntimeApiDataStoreManager, RuntimeApiDataStoreManager>();
builder.Services.AddSingleton<IThemeService, ThemeService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Reflection;
using System.Text.Json;
using Amazon.Lambda.TestTool.Configuration;

namespace Amazon.Lambda.TestTool.Utilities;

Expand Down Expand Up @@ -74,4 +75,32 @@ public static string TryPrettyPrintJson(string? data)
return data ?? string.Empty;
}
}

/// <summary>
/// Configures the web application builder with necessary services, configuration, and logging setup.
/// </summary>
/// <param name="builder">The WebApplicationBuilder instance to be configured</param>
/// <remarks>
/// This method performs the following configurations:
/// 1. Registers the current assembly as a singleton service
/// 2. Registers ConfigurationSetup as a singleton service
/// 3. Builds and applies custom configuration
/// 4. Sets up logging providers with console output
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Thrown when ConfigurationSetup service cannot be resolved from the service provider
/// </exception>
public static void ConfigureWebApplicationBuilder(WebApplicationBuilder builder)
{
builder.Services.AddSingleton(typeof(Assembly), typeof(ConfigurationSetup).Assembly);
builder.Services.AddSingleton<ConfigurationSetup>();

var configSetup = builder.Services.BuildServiceProvider().GetRequiredService<ConfigurationSetup>();
var configuration = configSetup.GetConfiguration();
builder.Configuration.AddConfiguration(configuration);

builder.Logging.ClearProviders();
builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
builder.Logging.AddConsole();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore": "Trace"
"Default": "Information"
}
},
"DetailedErrors": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Trace",
"Microsoft.AspNetCore": "Trace"
"Default": "Error"
}
},
"AllowedHosts": "*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;
using Amazon.Lambda.TestTool.Configuration;
using Moq;
using Xunit;

namespace Amazon.Lambda.TestTool.UnitTests.Configuration;

public class ConfigurationSetupTests
{
[Fact]
public void GetConfiguration_WithValidAssemblyLocation_ReturnsConfiguration()
{
// Arrange
var mockAssembly = new Mock<Assembly>();
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(testDirectory);

try
{
// Create test appsettings.json
File.WriteAllText(
Path.Combine(testDirectory, "appsettings.json"),
@"{""TestSetting"": ""TestValue""}"
);

mockAssembly
.Setup(x => x.Location)
.Returns(Path.Combine(testDirectory, "dummy.dll"));

var configSetup = new ConfigurationSetup(mockAssembly.Object);

// Act
var configuration = configSetup.GetConfiguration();

// Assert
Assert.NotNull(configuration);
Assert.Equal("TestValue", configuration["TestSetting"]);
}
finally
{
// Cleanup
Directory.Delete(testDirectory, true);
}
}

[Fact]
public void GetConfiguration_WithEnvironmentSpecificConfig_LoadsBothConfigs()
{
// Arrange
var mockAssembly = new Mock<Assembly>();
var testDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(testDirectory);

try
{
// Create test appsettings.json
File.WriteAllText(
Path.Combine(testDirectory, "appsettings.json"),
@"{""TestSetting"": ""BaseValue"", ""CommonSetting"": ""BaseValue""}"
);

// Create test appsettings.Development.json
File.WriteAllText(
Path.Combine(testDirectory, "appsettings.Development.json"),
@"{""TestSetting"": ""DevValue""}"
);

Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

mockAssembly
.Setup(x => x.Location)
.Returns(Path.Combine(testDirectory, "dummy.dll"));

var configSetup = new ConfigurationSetup(mockAssembly.Object);

// Act
var configuration = configSetup.GetConfiguration();

// Assert
Assert.NotNull(configuration);
Assert.Equal("DevValue", configuration["TestSetting"]); // Overridden value
Assert.Equal("BaseValue", configuration["CommonSetting"]); // Base value
}
finally
{
// Cleanup
Directory.Delete(testDirectory, true);
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
}
}

[Fact]
public void GetConfiguration_WithInvalidAssemblyLocation_ThrowsInvalidOperationException()
{
// Arrange
var mockAssembly = new Mock<Assembly>();
mockAssembly
.Setup(x => x.Location)
.Returns(string.Empty);

var configSetup = new ConfigurationSetup(mockAssembly.Object);

// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(
() => configSetup.GetConfiguration()
);
Assert.Equal("Unable to determine assembly location", exception.Message);
}
}

Loading