using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace Admin.Core.Serilog.Es
{
///
/// Json 配置文件通用类
///
public static class JsonConfigUtils
{
#region 变量
///
/// 锁
///
private static object __Lock__ = new object();
// 读取到的系统配置信息
public static IConfiguration Configuration { get; set; }
#endregion
///
/// 读取配置文件的信息
///
///
/// 要读取json的名称
/// 要读取的json节点名称
///
public static T GetAppSettings(string AppSettingsFileName, string key) where T : class, new()
{
lock (__Lock__)
{
if (Configuration == null)
{
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource
{
Path = AppSettingsFileName,
Optional = false,
ReloadOnChange = true
})
.Build();
}
var appconfig = new ServiceCollection()
.AddOptions()
.Configure(Configuration.GetSection(key))
.BuildServiceProvider()
.GetService>()
.Value;
return appconfig;
}
}
public static string GetJson(string jsonPath, string key)
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build(); //json文件地址
string s = config.GetSection(key).Value; //json某个对象
return s;
}
}
}