Table of Contents

Class CfgRootExtensions

Namespace
Apq.Cfg
Assembly
Apq.Cfg.dll

ICfgRoot 扩展方法

public static class CfgRootExtensions
Inheritance
CfgRootExtensions
Inherited Members

Methods

GetMasked(ICfgRoot, string)

获取脱敏后的配置值(用于日志输出)

public static string GetMasked(this ICfgRoot cfg, string key)

Parameters

cfg ICfgRoot

配置根

key string

配置键

Returns

string

脱敏后的值

Examples

// 日志输出时使用脱敏值
logger.LogInformation("连接字符串: {ConnectionString}", cfg.GetMasked("Database:ConnectionString"));
// 输出: 连接字符串: Ser***ion

GetMaskedSnapshot(ICfgRoot)

获取所有配置的脱敏快照(用于调试)

public static IReadOnlyDictionary<string, string> GetMaskedSnapshot(this ICfgRoot cfg)

Parameters

cfg ICfgRoot

配置根

Returns

IReadOnlyDictionary<string, string>

脱敏后的配置键值对字典

Examples

// 获取脱敏快照用于调试
var snapshot = cfg.GetMaskedSnapshot();
foreach (var (key, value) in snapshot)
{
    Console.WriteLine($"{key}: {value}");
}

GetRequired<T>(ICfgRoot, string)

获取必需的配置值,如果不存在则抛出异常

public static T GetRequired<T>(this ICfgRoot root, string key)

Parameters

root ICfgRoot
key string

Returns

T

Type Parameters

T

GetValue<T>(ICfgRoot, string, T?)

获取配置值,如果不存在则返回默认值

public static T? GetValue<T>(this ICfgRoot root, string key, T? defaultValue)

Parameters

root ICfgRoot
key string
defaultValue T

Returns

T

Type Parameters

T

Remarks

此方法是 GetValue<T>(string) 的重载版本, 允许指定自定义默认值。命名与 Microsoft.Extensions.Configuration 保持一致。

TryGetValue<T>(ICfgRoot, string, out T?)

尝试获取配置值

public static bool TryGetValue<T>(this ICfgRoot root, string key, out T? value)

Parameters

root ICfgRoot
key string
value T

Returns

bool

Type Parameters

T