# 简介
Apq.Cfg 是一个高性能的 .NET 配置组件库,旨在提供统一、灵活、高效的配置管理解决方案。
支持多种配置格式和配置中心:
完美集成 Microsoft.Extensions.DependencyInjection:
services.AddApqCfg(cfg => cfg
.AddJson("config.json", level: 0)
.AddEnvironmentVariables(level: 1, prefix: "APP_"));
// 使用 IOptions 模式
services.ConfigureApqCfg<DatabaseOptions>("Database");
清晰的接口设计,轻松实现自定义配置源:
public class MyCustomSource : ICfgSource
{
public int Level { get; }
public bool IsWriteable { get; }
public Task<IDictionary<string, string?>> LoadAsync(CancellationToken cancellationToken)
{
// 实现自定义加载逻辑
}
}
内置配置加密和脱敏功能,保护敏感信息:
var cfg = new CfgBuilder()
.AddJson("config.json", level: 0)
.AddAesGcmEncryptionFromEnv() // 自动解密 {ENC} 前缀的值
.AddSensitiveMasking() // 日志输出时自动脱敏
.Build();
// 读取时自动解密
var password = cfg["Database:Password"];
// 日志输出时脱敏
logger.LogInfo("密码: {0}", cfg.GetMasked("Database:Password"));
// 输出: 密码: myS***ord
支持变量引用,实现配置的动态组合和复用(Microsoft.Extensions.Configuration 不支持此功能):
// config.json: { "App:Name": "MyApp", "App:LogPath": "${App:Name}/logs" }
var cfg = new CfgBuilder()
.AddJson("config.json", level: 0)
.Build();
// 解析变量引用
var logPath = cfg.GetResolved("App:LogPath");
// 返回: "MyApp/logs"
// 引用环境变量和系统属性
var home = cfg.GetResolved("Paths:Home"); // ${ENV:USERPROFILE}
var machine = cfg.GetResolved("Paths:Machine"); // ${SYS:MachineName}
dotnet add package Apq.Cfg
using Apq.Cfg;
var cfg = new CfgBuilder()
.AddJson("config.json", level: 0, writeable: false)
.Build();
// 读取配置
var value = cfg["Section:Key"];
var typedValue = cfg.GetValue<int>("Section:IntKey");
| 框架 | 版本 |
|---|---|
| .NET | 6.0, 7.0, 8.0, 9.0 |
| .NET Standard | 2.0, 2.1 |