Skip to content

Commit 6d019ef

Browse files
authored
IGNITE-27423 .NET: Set LoggerFactory automatically in IgniteServiceCollectionExtensions (#7277)
1 parent 95e32e1 commit 6d019ef

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

modules/platforms/dotnet/Apache.Ignite.Tests/IgniteServiceCollectionExtensionsTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,55 @@ public void TestRegisterScopesConfigurationFuncWithKeyKeyed([Values] ServiceLife
153153
(s, key) => s.AddIgniteClientGroupKeyed(key, (_, _) => CreateGroupConfig(), lifetime));
154154
}
155155

156+
[Test]
157+
public void TestAutomaticLoggerFactorySetFromServices()
158+
{
159+
var services = new ServiceCollection();
160+
161+
var diLoggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
162+
services.AddSingleton(diLoggerFactory);
163+
164+
var config = new IgniteClientGroupConfiguration
165+
{
166+
ClientConfiguration = new IgniteClientConfiguration(_server.Endpoint)
167+
};
168+
169+
services.AddIgniteClientGroup(config);
170+
171+
using var serviceProvider = services.BuildServiceProvider();
172+
using var group = serviceProvider.GetRequiredService<IgniteClientGroup>();
173+
174+
var actualLoggerFactory = group.Configuration.ClientConfiguration.LoggerFactory;
175+
Assert.AreSame(diLoggerFactory, actualLoggerFactory);
176+
}
177+
178+
[Test]
179+
public void TestCustomLoggerFactoryIsPreserved()
180+
{
181+
var services = new ServiceCollection();
182+
183+
var diLoggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
184+
services.AddSingleton(diLoggerFactory);
185+
186+
var customLoggerFactory = TestUtils.GetConsoleLoggerFactory(LogLevel.Trace);
187+
var config = new IgniteClientGroupConfiguration
188+
{
189+
ClientConfiguration = new IgniteClientConfiguration(_server.Endpoint)
190+
{
191+
LoggerFactory = customLoggerFactory
192+
}
193+
};
194+
195+
services.AddIgniteClientGroup(config);
196+
197+
using var serviceProvider = services.BuildServiceProvider();
198+
using var group = serviceProvider.GetRequiredService<IgniteClientGroup>();
199+
200+
var actualLoggerFactory = group.Configuration.ClientConfiguration.LoggerFactory;
201+
Assert.AreSame(customLoggerFactory, actualLoggerFactory);
202+
Assert.AreNotSame(diLoggerFactory, actualLoggerFactory);
203+
}
204+
156205
private static async Task ValidateRegisterSingleClient(Action<ServiceCollection> register, Func<IServiceProvider, IgniteClientGroup?> resolve)
157206
{
158207
var services = new ServiceCollection();

modules/platforms/dotnet/Apache.Ignite/IgniteServiceCollectionExtensions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Apache.Ignite;
1818
using System;
1919
using Microsoft.Extensions.DependencyInjection;
2020
using Microsoft.Extensions.DependencyInjection.Extensions;
21+
using Microsoft.Extensions.Logging;
22+
using Microsoft.Extensions.Logging.Abstractions;
2123

2224
/// <summary>
2325
/// Extension methods for setting up Apache Ignite services
@@ -140,7 +142,30 @@ private static IServiceCollection AddIgniteClientGroupCore(
140142
services.TryAdd(new ServiceDescriptor(
141143
typeof(IgniteClientGroup),
142144
key,
143-
(sp, innerKey) => new IgniteClientGroup(configure(sp, innerKey)),
145+
(serviceProvider, innerKey) =>
146+
{
147+
IgniteClientGroupConfiguration cfg = configure(serviceProvider, innerKey);
148+
149+
if (cfg.ClientConfiguration.LoggerFactory == NullLoggerFactory.Instance)
150+
{
151+
// Use DI logger factory if none was provided.
152+
var diFactory = serviceProvider.GetService<ILoggerFactory>();
153+
154+
if (diFactory != null && diFactory is not NullLoggerFactory)
155+
{
156+
// Create a new config to avoid modifying the one provided by the user.
157+
cfg = cfg with
158+
{
159+
ClientConfiguration = cfg.ClientConfiguration with
160+
{
161+
LoggerFactory = diFactory
162+
}
163+
};
164+
}
165+
}
166+
167+
return new IgniteClientGroup(cfg);
168+
},
144169
clientGroupLifetime));
145170

146171
return services;

modules/platforms/dotnet/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ IJobExecution<int> jobExecution = await client.Compute.SubmitAsync(
6060
int wordCount = await jobExecution.GetResultAsync();
6161
```
6262

63+
## DI Integration
64+
65+
Use `IgniteServiceCollectionExtensions.AddIgniteClientGroup` to register Ignite client in the dependency injection container:
66+
67+
```cs
68+
var builder = WebApplication.CreateBuilder(args);
69+
70+
builder.Services.AddIgniteClientGroup(new IgniteClientGroupConfiguration
71+
{
72+
Size = 1, // Client pool size, 1 is enough for most scenarios.
73+
ClientConfiguration = new IgniteClientConfiguration("localhost:10942")
74+
});
75+
```
76+
77+
Inject `IgniteClientGroup` where needed:
78+
79+
```cs
80+
app.MapGet("/tables", async ([FromServices] IgniteClientGroup igniteGrp) =>
81+
{
82+
IIgnite ignite = await igniteGrp.GetIgniteAsync();
83+
84+
return await ignite.Tables.GetTablesAsync();
85+
});
86+
```
87+
6388
# API Walkthrough
6489

6590
## Configuration

0 commit comments

Comments
 (0)