using DTS.Common.Utilities.Logging;
using System;
using System.Configuration;
using System.Linq;
// ReSharper disable InconsistentNaming
// ReSharper disable EmptyConstructor
namespace DTS.Common.Core.Config
{
public static class DTSConfig
{
private static string AltConfigPath;
public static string AltConfigPathGet()
{
lock (MyLock)
{
return AltConfigPath;
}
}
public static void AltConfigPathSet(string path)
{
lock (MyLock)
{
AltConfigPath = path;
}
}
// static constructor
static DTSConfig()
{
}
public static void DTSConfigInit(string path)
{
SetAltConfigPath(path);
}
private static object MyLock = new object();
///
/// Static variable to hold alternate configuration file
///
private static Configuration AltConfig;
public static Configuration GetAltConfig()
{
lock (MyLock)
{
return AltConfig;
}
}
///
/// Static variable to hold alternate configuration file
///
///
///
///
public static void SetAltConfigPath(string path)
{
lock (MyLock)
{
AltConfigPath = path;
var dtsConfig = new ExeConfigurationFileMap
{
ExeConfigFilename = string.IsNullOrEmpty(AltConfigPath) ? DTSConstants.CustomConfigPath : AltConfigPath
};
AltConfig = ConfigurationManager.OpenMappedExeConfiguration(dtsConfig, ConfigurationUserLevel.None, true);
}
}
///
/// Static method to retrieve a setting from config file
///
/// string
/// string
public static string GetAppSetting(string key)
{
var appSetting = AltConfig.AppSettings.Settings.Cast().FirstOrDefault(setting => setting.Key == key);
if (appSetting == null)
{
APILogger.Log(
$"## AltConfig.AppSettings.Settings.Cast().FirstOrDefault(setting => setting.Key == key) is null for key={key}");
}
return appSetting == null ? string.Empty : appSetting.Value;
}
///
/// Static method to get a section from config file. Used by plugin code to get plugin library section.
///
/// string
/// object
public static object GetSection(string sectionName)
{
var section = AltConfig.GetSection(sectionName);
if (section == null)
{
APILogger.Log($"## AltConfig.GetSection(sectionName) is null for sectionName={sectionName}");
}
return section;
}
}
}