|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
@ -10,11 +11,8 @@ namespace HybirdFrameworkCore.Configuration;
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AppSettingsHelper
|
|
|
|
|
{
|
|
|
|
|
public AppSettingsHelper(string contentPath)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IConfiguration Configuration { get; set; }
|
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(AppSettingsHelper));
|
|
|
|
|
private static IConfiguration? Configuration { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 封装要操作的字符
|
|
|
|
@ -22,21 +20,35 @@ public class AppSettingsHelper
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sections">节点配置</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetContent(params string[] sections)
|
|
|
|
|
public static string? GetContent(params string[] sections)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Configuration == null)
|
|
|
|
|
{
|
|
|
|
|
string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
|
|
|
|
|
if (activeProfile == null)
|
|
|
|
|
{
|
|
|
|
|
activeProfile = "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activeProfile += ".";
|
|
|
|
|
}
|
|
|
|
|
var path = "appsettings."+ activeProfile+ "json";
|
|
|
|
|
Log.Info($"read path={path}");
|
|
|
|
|
var contentPath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
var Path = "appsettings.json";
|
|
|
|
|
Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource
|
|
|
|
|
{ Path = Path, Optional = false, ReloadOnChange = true }).Build();
|
|
|
|
|
{ Path = path, Optional = false, ReloadOnChange = true }).Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sections.Any()) return Configuration[string.Join(":", sections)];
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool SetContent(string value, params string[] sections)
|
|
|
|
@ -44,9 +56,18 @@ public class AppSettingsHelper
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var contentPath = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
var Path = contentPath + "appsettings.json";
|
|
|
|
|
string? activeProfile = Environment.GetEnvironmentVariable("profiles_active");
|
|
|
|
|
if (activeProfile == null)
|
|
|
|
|
{
|
|
|
|
|
activeProfile = "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
activeProfile += ".";
|
|
|
|
|
}
|
|
|
|
|
var path = "appsettings."+ activeProfile+ "json";
|
|
|
|
|
JObject jsonObject;
|
|
|
|
|
using (var file = new StreamReader(Path))
|
|
|
|
|
using (var file = new StreamReader(path))
|
|
|
|
|
using (var reader = new JsonTextReader(file))
|
|
|
|
|
{
|
|
|
|
|
jsonObject = (JObject)JToken.ReadFrom(reader);
|
|
|
|
@ -54,7 +75,7 @@ public class AppSettingsHelper
|
|
|
|
|
jsonObject[sections[0]][sections[1]] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var writer = new StreamWriter(Path))
|
|
|
|
|
using (var writer = new StreamWriter(path))
|
|
|
|
|
using (var jsonwriter = new JsonTextWriter(writer))
|
|
|
|
|
{
|
|
|
|
|
jsonObject.WriteTo(jsonwriter);
|
|
|
|
@ -67,4 +88,24 @@ public class AppSettingsHelper
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sections"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool GetBool(params string[] sections)
|
|
|
|
|
{
|
|
|
|
|
string s = GetContent(sections);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(s))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (bool.TryParse(s, out var result))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|