Skip to content

Commit d3cba9a

Browse files
authored
Add WithLogLevel extension method (#884)
* feat(surrealdb): add WithLogLevel extension method * feat(goff): add WithLogLevel extension method * docs(goff): list supported log levels
1 parent ada053b commit d3cba9a

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

src/CommunityToolkit.Aspire.Hosting.GoFeatureFlag/GoFeatureFlagBuilderExtensions.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Aspire.Hosting.ApplicationModel;
55
using Aspire.Hosting.Utils;
66
using CommunityToolkit.Aspire.Hosting.GoFeatureFlag;
7+
using Microsoft.Extensions.Logging;
78

89
namespace Aspire.Hosting;
910

@@ -55,11 +56,11 @@ public static IResourceBuilder<GoFeatureFlagResource> AddGoFeatureFlag(
5556
return builder.AddResource(goFeatureFlagResource)
5657
.WithImage(GoFeatureFlagContainerImageTags.Image, GoFeatureFlagContainerImageTags.Tag)
5758
.WithImageRegistry(GoFeatureFlagContainerImageTags.Registry)
58-
.WithHttpEndpoint(targetPort: GoFeatureFlagPort, port: port, name: GoFeatureFlagResource.PrimaryEndpointName)
59+
.WithHttpEndpoint(targetPort: GoFeatureFlagPort, port: port,
60+
name: GoFeatureFlagResource.PrimaryEndpointName)
5961
.WithHttpHealthCheck("/health")
6062
.WithEntrypoint("/go-feature-flag")
61-
.WithArgs(args)
62-
.WithOtlpExporter();
63+
.WithArgs(args);
6364
}
6465

6566
/// <summary>
@@ -120,4 +121,33 @@ public static IResourceBuilder<GoFeatureFlagResource> WithGoffBindMount(this IRe
120121

121122
return builder.WithBindMount(source, "/goff");
122123
}
124+
125+
/// <summary>
126+
/// Configures logging level for the GO Feature Flag container resource.
127+
/// </summary>
128+
/// <param name="builder">The resource builder.</param>
129+
/// <param name="logLevel">The log level to set.</param>
130+
/// <returns>The <see cref="IResourceBuilder{GoFeatureFlagResource}"/>.</returns>
131+
/// <remarks>
132+
/// The only supported <see cref="LogLevel"/> by GO Feature Flag are <see cref="LogLevel.Error"/>,
133+
/// <see cref="LogLevel.Warning"/>, <see cref="LogLevel.Information"/> and <see cref="LogLevel.Debug"/>.
134+
/// </remarks>
135+
public static IResourceBuilder<GoFeatureFlagResource> WithLogLevel(
136+
this IResourceBuilder<GoFeatureFlagResource> builder,
137+
LogLevel logLevel
138+
)
139+
{
140+
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
141+
142+
string value = logLevel switch
143+
{
144+
LogLevel.Error => "ERROR",
145+
LogLevel.Warning => "WARN",
146+
LogLevel.Information => "INFO",
147+
LogLevel.Debug => "DEBUG",
148+
_ => throw new ArgumentOutOfRangeException(nameof(logLevel), "This log level is not supported by GO Feature Flag.")
149+
};
150+
151+
return builder.WithEnvironment("LOGLEVEL", value);
152+
}
123153
}

src/CommunityToolkit.Aspire.Hosting.SurrealDb/SurrealDbBuilderExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ public static IResourceBuilder<SurrealDbServerResource> WithInitFiles(this IReso
427427
context.EnvironmentVariables[ImportFileEnvVarName] = initFilePath;
428428
});
429429
}
430+
431+
/// <summary>
432+
/// Configures logging level for the SurrealDB container resource.
433+
/// </summary>
434+
/// <param name="builder">The resource builder.</param>
435+
/// <param name="logLevel">The log level to set.</param>
436+
/// <returns>The <see cref="IResourceBuilder{SurrealDbServerResource}"/>.</returns>
437+
public static IResourceBuilder<SurrealDbServerResource> WithLogLevel(
438+
this IResourceBuilder<SurrealDbServerResource> builder,
439+
LogLevel logLevel
440+
)
441+
{
442+
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
443+
444+
string value = logLevel switch
445+
{
446+
LogLevel.Critical => "full",
447+
LogLevel.Information => "info",
448+
LogLevel.Warning => "warn",
449+
_ => logLevel.ToString().ToLowerInvariant()
450+
};
451+
452+
return builder.WithEnvironment("SURREAL_LOG", value);
453+
}
430454

431455
/// <summary>
432456
/// Adds a Surrealist UI instance for SurrealDB to the application model.

tests/CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests/AddGoFeatureFlagTests.cs

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

44
using System.Net.Sockets;
55
using Aspire.Hosting;
6+
using Microsoft.Extensions.Logging;
67

78
namespace CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests;
89

@@ -89,4 +90,42 @@ public async Task GoFeatureFlagCreatesConnectionString()
8990
Assert.Equal($"Endpoint=http://localhost:27020", connectionString);
9091
Assert.Equal("Endpoint=http://{goff.bindings.http.host}:{goff.bindings.http.port}", connectionStringResource.ConnectionStringExpression.ValueExpression);
9192
}
93+
94+
[Theory]
95+
[InlineData(LogLevel.Debug, "DEBUG")]
96+
[InlineData(LogLevel.Information, "INFO")]
97+
[InlineData(LogLevel.Warning, "WARN")]
98+
[InlineData(LogLevel.Error, "ERROR")]
99+
public async Task AddSurrealServerContainerWithLogLevel(LogLevel logLevel, string? expected)
100+
{
101+
var appBuilder = DistributedApplication.CreateBuilder();
102+
103+
var goff = appBuilder
104+
.AddGoFeatureFlag("goff")
105+
.WithLogLevel(logLevel);
106+
107+
using var app = appBuilder.Build();
108+
109+
var config = await goff.Resource.GetEnvironmentVariableValuesAsync();
110+
111+
bool hasValue = config.TryGetValue("LOGLEVEL", out var value);
112+
113+
Assert.True(hasValue);
114+
Assert.Equal(expected, value);
115+
}
116+
117+
[Theory]
118+
[InlineData(LogLevel.Trace)]
119+
[InlineData(LogLevel.Critical)]
120+
[InlineData(LogLevel.None)]
121+
public void AddSurrealServerContainerWithLogLevelThrowsOnUnsupportedLogLevel(LogLevel logLevel)
122+
{
123+
var appBuilder = DistributedApplication.CreateBuilder();
124+
125+
var func = () => appBuilder
126+
.AddGoFeatureFlag("goff")
127+
.WithLogLevel(logLevel);
128+
129+
Assert.Throws<ArgumentOutOfRangeException>(func);
130+
}
92131
}

tests/CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests/GoFeatureFlagPublicApiTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Aspire.Hosting;
5+
using Microsoft.Extensions.Logging;
56

67
namespace CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests;
78

@@ -79,4 +80,17 @@ public void CtorGoFeatureFlagResourceShouldThrowWhenNameIsNull()
7980
var exception = Assert.Throws<ArgumentNullException>(action);
8081
Assert.Equal(nameof(name), exception.ParamName);
8182
}
83+
84+
[Fact]
85+
public void WithLogLevelShouldThrowWhenBuilderIsNull()
86+
{
87+
IResourceBuilder<GoFeatureFlagResource> builder = null!;
88+
89+
#pragma warning disable CTASPIRE002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
90+
var action = () => builder.WithLogLevel(LogLevel.Trace);
91+
#pragma warning restore CTASPIRE002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
92+
93+
var exception = Assert.Throws<ArgumentNullException>(action);
94+
Assert.Equal(nameof(builder), exception.ParamName);
95+
}
8296
}

tests/CommunityToolkit.Aspire.Hosting.SurrealDb.Tests/AddSurrealServerTests.cs

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

44
using Aspire.Hosting;
55
using Aspire.Hosting.Utils;
6+
using Microsoft.Extensions.Logging;
67
using System.Net.Sockets;
78

89
namespace CommunityToolkit.Aspire.Hosting.SurrealDb.Tests;
@@ -180,4 +181,30 @@ public void CanAddDatabasesWithTheSameNameOnMultipleServers()
180181
Assert.Equal("{ns1.connectionString};Database=imports", db1.Resource.ConnectionStringExpression.ValueExpression);
181182
Assert.Equal("{ns2.connectionString};Database=imports", db2.Resource.ConnectionStringExpression.ValueExpression);
182183
}
184+
185+
[Theory]
186+
[InlineData(LogLevel.Trace, "trace")]
187+
[InlineData(LogLevel.Debug, "debug")]
188+
[InlineData(LogLevel.Information, "info")]
189+
[InlineData(LogLevel.Warning, "warn")]
190+
[InlineData(LogLevel.Error, "error")]
191+
[InlineData(LogLevel.Critical, "full")]
192+
[InlineData(LogLevel.None, "none")]
193+
public async Task AddSurrealServerContainerWithLogLevel(LogLevel logLevel, string expected)
194+
{
195+
var appBuilder = DistributedApplication.CreateBuilder();
196+
197+
var surrealServer = appBuilder
198+
.AddSurrealServer("surreal")
199+
.WithLogLevel(logLevel);
200+
201+
using var app = appBuilder.Build();
202+
203+
var config = await surrealServer.Resource.GetEnvironmentVariableValuesAsync();
204+
205+
bool hasValue = config.TryGetValue("SURREAL_LOG", out var value);
206+
207+
Assert.True(hasValue);
208+
Assert.Equal(expected, value);
209+
}
183210
}

tests/CommunityToolkit.Aspire.Hosting.SurrealDb.Tests/SurrealDbPublicApiTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Aspire.Hosting;
55
using Aspire.Hosting.Utils;
6+
using Microsoft.Extensions.Logging;
67

78
namespace CommunityToolkit.Aspire.Hosting.SurrealDb.Tests;
89

@@ -121,7 +122,6 @@ public void WithDataBindMountShouldThrowWhenSourceIsNull()
121122
Assert.Equal(nameof(source), exception.ParamName);
122123
}
123124

124-
125125
[Fact(Skip = "Feature is unstable and blocking the release")]
126126
public void WithInitFilesShouldThrowWhenBuilderIsNull()
127127
{
@@ -154,6 +154,19 @@ public void WithInitFilesShouldThrowWhenSourceIsNullOrEmpty(bool isNull)
154154
: Assert.Throws<ArgumentException>(action);
155155
Assert.Equal(nameof(source), exception.ParamName);
156156
}
157+
158+
[Fact]
159+
public void WithLogLevelShouldThrowWhenBuilderIsNull()
160+
{
161+
IResourceBuilder<SurrealDbServerResource> builder = null!;
162+
163+
#pragma warning disable CTASPIRE002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
164+
var action = () => builder.WithLogLevel(LogLevel.Trace);
165+
#pragma warning restore CTASPIRE002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
166+
167+
var exception = Assert.Throws<ArgumentNullException>(action);
168+
Assert.Equal(nameof(builder), exception.ParamName);
169+
}
157170

158171
[Fact]
159172
public void WithSurrealistShouldThrowWhenBuilderIsNull()

0 commit comments

Comments
 (0)