init
This commit is contained in:
100
Common/DTS.Common.Core/Config/DTSConfig.cs
Normal file
100
Common/DTS.Common.Core/Config/DTSConfig.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
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();
|
||||
/// <summary>
|
||||
/// Static variable to hold alternate configuration file
|
||||
/// </summary>
|
||||
private static Configuration AltConfig;
|
||||
|
||||
public static Configuration GetAltConfig()
|
||||
{
|
||||
lock (MyLock)
|
||||
{
|
||||
return AltConfig;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static variable to hold alternate configuration file
|
||||
/// </summary>
|
||||
/// <history>
|
||||
///
|
||||
/// </history>
|
||||
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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Static method to retrieve a setting from config file
|
||||
/// </summary>
|
||||
/// <param name="key">string</param>
|
||||
/// <returns>string</returns>
|
||||
public static string GetAppSetting(string key)
|
||||
{
|
||||
var appSetting = AltConfig.AppSettings.Settings.Cast<KeyValueConfigurationElement>().FirstOrDefault(setting => setting.Key == key);
|
||||
if (appSetting == null)
|
||||
{
|
||||
APILogger.Log(
|
||||
$"## AltConfig.AppSettings.Settings.Cast<KeyValueConfigurationElement>().FirstOrDefault(setting => setting.Key == key) is null for key={key}");
|
||||
}
|
||||
return appSetting == null ? string.Empty : appSetting.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static method to get a section from config file. Used by plugin code to get plugin library section.
|
||||
/// </summary>
|
||||
/// <param name="sectionName">string</param>
|
||||
/// <returns>object</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user