Skip to content

Commit d9e8e95

Browse files
authored
Enable Redis Logging (dotnet#597)
* Update Redis dependencies - the client library and otel packages have new versions, update to the latest - SE.Redis have ILogger support now, update to use it
1 parent e7762a4 commit d9e8e95

File tree

4 files changed

+8
-18
lines changed

4 files changed

+8
-18
lines changed

src/Components/Aspire.StackExchange.Redis/AspireRedisExtensions.cs

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

44
global using System.Net.Security; // needed to work around https://github.com/dotnet/runtime/issues/94065
55

6-
using System.Text;
76
using Aspire;
87
using Aspire.StackExchange.Redis;
98
using Microsoft.Extensions.Configuration;
@@ -89,12 +88,12 @@ private static void AddRedis(IHostApplicationBuilder builder, string configurati
8988
if (serviceKey is null)
9089
{
9190
builder.Services.AddSingleton<IConnectionMultiplexer>(
92-
sp => ConnectionMultiplexer.Connect(GetConfigurationOptions(sp, connectionName, configurationSectionName, optionsName), CreateLogger(sp)));
91+
sp => ConnectionMultiplexer.Connect(GetConfigurationOptions(sp, connectionName, configurationSectionName, optionsName)));
9392
}
9493
else
9594
{
9695
builder.Services.AddKeyedSingleton<IConnectionMultiplexer>(serviceKey,
97-
(sp, key) => ConnectionMultiplexer.Connect(GetConfigurationOptions(sp, connectionName, configurationSectionName, optionsName), CreateLogger(sp)));
96+
(sp, key) => ConnectionMultiplexer.Connect(GetConfigurationOptions(sp, connectionName, configurationSectionName, optionsName)));
9897
}
9998

10099
if (settings.Tracing)
@@ -120,11 +119,6 @@ private static void AddRedis(IHostApplicationBuilder builder, string configurati
120119
connectionMultiplexerFactory: sp => serviceKey is null ? sp.GetRequiredService<IConnectionMultiplexer>() : sp.GetRequiredKeyedService<IConnectionMultiplexer>(serviceKey),
121120
healthCheckName));
122121
}
123-
124-
static TextWriter? CreateLogger(IServiceProvider serviceProvider)
125-
=> serviceProvider.GetService<ILoggerFactory>() is { } loggerFactory
126-
? new LoggingTextWriter(loggerFactory.CreateLogger("Aspire.StackExchange.Redis"))
127-
: null;
128122
}
129123

130124
private static ConfigurationOptions GetConfigurationOptions(IServiceProvider serviceProvider, string connectionName, string configurationSectionName, string? optionsName)
@@ -138,6 +132,9 @@ private static ConfigurationOptions GetConfigurationOptions(IServiceProvider ser
138132
throw new InvalidOperationException($"No endpoints specified. Ensure a valid connection string was provided in 'ConnectionStrings:{connectionName}' or for the '{configurationSectionName}:ConnectionString' configuration key.");
139133
}
140134

135+
// ensure the LoggerFactory is initialized if someone hasn't already set it.
136+
configurationOptions.LoggerFactory ??= serviceProvider.GetService<ILoggerFactory>();
137+
141138
return configurationOptions;
142139
}
143140

@@ -149,13 +146,6 @@ private static ConfigurationOptions BindToConfiguration(ConfigurationOptions opt
149146
return options;
150147
}
151148

152-
private sealed class LoggingTextWriter(ILogger logger) : TextWriter
153-
{
154-
public override Encoding Encoding => Encoding.UTF8;
155-
156-
public override void Write(string? value) => logger.LogTrace(value);
157-
}
158-
159149
/// <summary>
160150
/// ConfigurationOptionsFactory parses a ConfigurationOptions options object from Configuration.
161151
/// </summary>

src/Components/Aspire.StackExchange.Redis/ConfigurationSchema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"definitions": {
33
"logLevel": {
44
"properties": {
5-
"Aspire.StackExchange.Redis": {
5+
"StackExchange.Redis": {
66
"$ref": "#/definitions/logLevelThreshold"
77
}
88
}

src/Components/Telemetry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Aspire.RabbitMQ.Client:
220220

221221
Aspire.StackExchange.Redis:
222222
- Log categories:
223-
- "Aspire.StackExchange.Redis" (this name is defined by our component, we can change it)
223+
- "StackExchange.Redis"
224224
- Activity source names:
225225
- "OpenTelemetry.Instrumentation.StackExchangeRedis"
226226
- Metric names:

tests/Aspire.StackExchange.Redis.Tests/ConformanceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ConformanceTests : ConformanceTests<IConnectionMultiplexer, StackEx
2020

2121
protected override bool SupportsKeyedRegistrations => true;
2222

23-
protected override string[] RequiredLogCategories => new string[] { "Aspire.StackExchange.Redis" };
23+
protected override string[] RequiredLogCategories => new string[] { "StackExchange.Redis" };
2424

2525
// https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/e4cb523a4a3592e1a1adf30f3596025bfd8978e3/src/OpenTelemetry.Instrumentation.StackExchangeRedis/StackExchangeRedisConnectionInstrumentation.cs#L34
2626
protected override string ActivitySourceName => "OpenTelemetry.Instrumentation.StackExchangeRedis";

0 commit comments

Comments
 (0)