Skip to content

Commit a689dcc

Browse files
Add AzureClientsCore overload with enableLogging parameter (#30317)
* Add AzureClientsCore overload with enableLogging parameter * Rename
1 parent 796d311 commit a689dcc

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

sdk/extensions/Microsoft.Extensions.Azure/api/Microsoft.Extensions.Azure.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static partial class AzureClientServiceCollectionExtensions
3030
{
3131
public static void AddAzureClients(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, System.Action<Microsoft.Extensions.Azure.AzureClientFactoryBuilder> configureClients) { }
3232
public static void AddAzureClientsCore(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection) { }
33+
public static void AddAzureClientsCore(this Microsoft.Extensions.DependencyInjection.IServiceCollection collection, bool enableLogForwarding) { }
3334
}
3435
public abstract partial class AzureComponentFactory
3536
{

sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientServiceCollectionExtensions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class AzureClientServiceCollectionExtensions
1717
{
1818
/// <summary>
1919
/// Adds the <see cref="IAzureClientFactory{TClient}"/> and related services to the <see cref="IServiceCollection"/>.
20+
/// Azure SDK logging is enabled once the configured client is created.
2021
/// </summary>
2122
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
2223
/// <param name="configureClients">An <see cref="AzureClientFactoryBuilder"/> that can be used to configure the client.</param>
@@ -28,12 +29,36 @@ public static void AddAzureClients(this IServiceCollection collection, Action<Az
2829

2930
/// <summary>
3031
/// Adds the minimum essential Azure SDK interop services like <see cref="AzureEventSourceLogForwarder"/> and <see cref="AzureComponentFactory"/> to the specified <see cref="IServiceCollection"/> without registering any client types.
32+
/// Azure SDK log forwarding to to <see cref="ILogger"/> will not be enabled by default, but can be enabled by calling the <see cref="AzureEventSourceLogForwarder.Start"/> method.
33+
/// Alternatively, you can use the <see cref="AddAzureClientsCore(Microsoft.Extensions.DependencyInjection.IServiceCollection, bool)"/> overload
34+
/// and pass <value>true</value> to enable log forwarding.
3135
/// </summary>
3236
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
3337
public static void AddAzureClientsCore(this IServiceCollection collection)
38+
{
39+
collection.AddAzureClientsCore(false);
40+
}
41+
42+
/// <summary>
43+
/// Adds the minimum essential Azure SDK interop services like <see cref="AzureEventSourceLogForwarder"/> and <see cref="AzureComponentFactory"/> to the specified <see cref="IServiceCollection"/> without registering any client types.
44+
/// </summary>
45+
/// <param name="collection">The <see cref="IServiceCollection"/>.</param>
46+
/// <param name="enableLogForwarding">Whether or not to enable Azure SDK log forwarding to <see cref="ILogger"/>. Even if this is set to <value>false</value>,
47+
/// log forwarding can be enabled by calling the <see cref="AzureEventSourceLogForwarder.Start"/> method.</param>
48+
public static void AddAzureClientsCore(this IServiceCollection collection, bool enableLogForwarding)
3449
{
3550
collection.AddOptions();
36-
collection.TryAddSingleton<AzureEventSourceLogForwarder>();
51+
collection.TryAddSingleton<AzureEventSourceLogForwarder>(provider =>
52+
{
53+
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
54+
var forwarder = new AzureEventSourceLogForwarder(loggerFactory);
55+
if (enableLogForwarding)
56+
{
57+
forwarder.Start();
58+
}
59+
60+
return forwarder;
61+
});
3762
collection.TryAddSingleton<ILoggerFactory, NullLoggerFactory>();
3863
collection.TryAddSingleton<AzureComponentFactory, AzureComponentFactoryImpl>();
3964
}

sdk/extensions/Microsoft.Extensions.Azure/tests/AzureComponentFactoryTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System.Collections.Generic;
5+
using Azure.Core.Diagnostics;
56
using Azure.Identity;
67
using Microsoft.Extensions.Azure;
78
using Microsoft.Extensions.Configuration;
@@ -131,6 +132,32 @@ public void GlobalOptionsAppliedToAzureComponentFactoryCreateClientOptions()
131132
Assert.AreEqual("AppId", options.Diagnostics.ApplicationId);
132133
}
133134

135+
[Test]
136+
[TestCase(true)]
137+
[TestCase(false)]
138+
public void EnableLoggingRespectedWhenCallingAddAzureClientsCore(bool enableLogging)
139+
{
140+
var configuration = GetConfiguration(
141+
new KeyValuePair<string, string>("TestClient:uri", "http://localhost/"));
142+
143+
var serviceCollection = new ServiceCollection();
144+
serviceCollection.AddAzureClientsCore(enableLogging);
145+
146+
ServiceProvider provider = serviceCollection.BuildServiceProvider();
147+
AzureComponentFactory factory = provider.GetService<AzureComponentFactory>();
148+
TestClientWithCredentials client = (TestClientWithCredentials) factory.CreateClient(typeof(TestClientWithCredentials), configuration.GetSection("TestClient"), new EnvironmentCredential(), new TestClientOptions());
149+
150+
Assert.AreEqual("http://localhost/", client.Uri.ToString());
151+
Assert.IsInstanceOf<EnvironmentCredential>(client.Credential);
152+
153+
AzureEventSourceLogForwarder forwarder = provider.GetService<AzureEventSourceLogForwarder>();
154+
var listener = (AzureEventSourceListener) forwarder.GetType().GetField(
155+
"_listener",
156+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
157+
.GetValue(forwarder);
158+
Assert.AreEqual(enableLogging, listener != null);
159+
}
160+
134161
private IConfiguration GetConfiguration(params KeyValuePair<string, string>[] items)
135162
{
136163
return new ConfigurationBuilder().AddInMemoryCollection(items).Build();

0 commit comments

Comments
 (0)