-
Notifications
You must be signed in to change notification settings - Fork 246
Add a way to get configuration section associated with logger provider #706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
/// <summary> | ||
/// Allows access to configuration section associated with logger provider | ||
/// </summary> | ||
/// <typeparam name="T">Type of logger provider to get configuration for</typeparam> | ||
public interface ILoggerProviderConfiguration<T> | ||
{ | ||
/// <summary> | ||
/// Configuration section for requested logger provider | ||
/// </summary> | ||
IConfiguration Configuration { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
/// <summary> | ||
/// Allows access to configuration section associated with logger provider | ||
/// </summary> | ||
public interface ILoggerProviderConfigurationFactory | ||
{ | ||
/// <summary> | ||
/// Return configuration section associated with logger provider | ||
/// </summary> | ||
/// <param name="providerType">The logger provider type</param> | ||
IConfiguration GetConfiguration(Type providerType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
internal class LoggerProviderConfiguration<T> : ILoggerProviderConfiguration<T> | ||
{ | ||
public LoggerProviderConfiguration(ILoggerProviderConfigurationFactory providerConfigurationFactory) | ||
{ | ||
Configuration = providerConfigurationFactory.GetConfiguration(typeof(T)); | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
internal class LoggerProviderConfigurationFactory : ILoggerProviderConfigurationFactory | ||
{ | ||
private readonly IEnumerable<LoggingConfiguration> _configurations; | ||
|
||
public LoggerProviderConfigurationFactory(IEnumerable<LoggingConfiguration> configurations) | ||
{ | ||
_configurations = configurations; | ||
} | ||
|
||
public IConfiguration GetConfiguration(Type providerType) | ||
{ | ||
if (providerType == null) | ||
{ | ||
throw new ArgumentNullException(nameof(providerType)); | ||
} | ||
|
||
var fullName = providerType.FullName; | ||
var alias = ProviderAliasUtilities.GetAlias(providerType); | ||
var configurationBuilder = new ConfigurationBuilder(); | ||
foreach (var configuration in _configurations) | ||
{ | ||
var sectionFromFullName = configuration.Configuration.GetSection(fullName); | ||
if (sectionFromFullName.Exists()) | ||
{ | ||
configurationBuilder.AddConfiguration(sectionFromFullName); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(alias)) | ||
{ | ||
var sectionFromAlias = configuration.Configuration.GetSection(alias); | ||
if (sectionFromAlias.Exists()) | ||
{ | ||
configurationBuilder.AddConfiguration(sectionFromAlias); | ||
} | ||
} | ||
} | ||
return configurationBuilder.Build(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
/// <inheritdoc /> | ||
public class LoggerProviderOptionsChangeTokenSource<TOptions, TProvider> : ConfigurationChangeTokenSource<TOptions> | ||
{ | ||
/// <inheritdoc /> | ||
public LoggerProviderOptionsChangeTokenSource(ILoggerProviderConfiguration<TProvider> providerConfiguration) : base(providerConfiguration.Configuration) | ||
{ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
/// <summary> | ||
/// Extension methods for setting up logging services in an <see cref="ILoggingBuilder" />. | ||
/// </summary> | ||
public static class LoggingBuilderConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds services required to consume <see cref="ILoggerProviderConfigurationFactory"/> or <see cref="ILoggerProviderConfiguration{T}"/> | ||
/// </summary> | ||
public static void AddConfiguration(this ILoggingBuilder builder) | ||
{ | ||
builder.Services.TryAddSingleton<ILoggerProviderConfigurationFactory, LoggerProviderConfigurationFactory>(); | ||
builder.Services.TryAddSingleton(typeof(ILoggerProviderConfiguration<>), typeof(LoggerProviderConfiguration<>)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Extensions.Logging.Configuration | ||
{ | ||
internal class LoggingConfiguration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so we don't assume that we can just take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I don't want random IConfiguration's in DI, especially if they were passed into logging. |
||
{ | ||
public IConfiguration Configuration { get; } | ||
|
||
public LoggingConfiguration(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Logging.Console | ||
{ | ||
internal class ConsoleLoggerOptionsSetup : ConfigureFromConfigurationOptions<ConsoleLoggerOptions> | ||
{ | ||
public ConsoleLoggerOptionsSetup(ILoggerProviderConfiguration<ConsoleLoggerProvider> providerConfiguration) | ||
: base(providerConfiguration.Configuration) | ||
{ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.Extensions.Logging.Configuration, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
internal class ProviderAliasUtilities | ||
{ | ||
private const string AliasAttibuteTypeFullName = "Microsoft.Extensions.Logging.ProviderAliasAttribute"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is by name so that 3rd party providers don't have to take an unneeded dependency ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of them cannot build against aspnetcore 2.0 (AI) |
||
private const string AliasAttibuteAliasProperty = "Alias"; | ||
|
||
internal static string GetAlias(Type providerType) | ||
{ | ||
foreach (var attribute in providerType.GetTypeInfo().GetCustomAttributes(inherit: false)) | ||
{ | ||
if (attribute.GetType().FullName == AliasAttibuteTypeFullName) | ||
{ | ||
var valueProperty = attribute | ||
.GetType() | ||
.GetProperty(AliasAttibuteAliasProperty, BindingFlags.Public | BindingFlags.Instance); | ||
|
||
if (valueProperty != null) | ||
{ | ||
return valueProperty.GetValue(attribute) as string; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty weird. Usually configuration is composed at the provider level not by using multiple IConfiguration instances. Why do we do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We allow composition of logging configurations for light-up scenarios (AI is adding another logging config file), so when you call
AddConfiguration
multiple times filtering rules get flattened in one list. I just simulated the same behavior for configuration sections.