Sources/Domenici.Utilities.Configuration/Configuration/SettingsManager.cs
/// <summary>
/// Load all sections and settings in memory.
/// </summary>
Load all sections and settings in memory.
Load all sections and settings in memory .
private static void LoadSettings()
        { 
            string message = "Start loading application settings data...";
            Debug.WriteLine(message);
            long ticks = DateTime.Now.Ticks;
            XmlDocument doc = null;
            #region Validation: Throw an exception is settings files cannot be found. 
            string defaultPath = ConfigurationManager.AppSettings[AppSettingsPathKeyName];
            if (String.IsNullOrEmpty(defaultPath))
                throw new SettingsException($"Missing key '{AppSettingsPathKeyName}' in the appSettings of the default configuration file.", null);
            if (!Path.IsPathRooted(defaultPath))
            {
                defaultPath = Path.GetFullPath(defaultPath);
            }
            if (!System.IO.Directory.Exists(defaultPath))
            {
                throw new SettingsException($"Missing configuration folder. The folder '{defaultPath}' in key '{AppSettingsPathKeyName}' in the appSettings of the configuration file does not exist.", null);
            }
            #endregion
            try
            {
                settingsList = new List<SettingProperties>();
                SettingsLoader loader = new SettingsLoader(settingsList, SectionSeparator);
                foreach (string file in Directory.GetFiles(defaultPath, "*.appsettings"))
                {
                    Console.WriteLine($"Loading application settings data on file: {file}");
                    loader.LoadSettings(file);
                    Console.WriteLine($"Done loading application settings data on file: {file}");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                throw new SettingsException("Unexpected error while loading application settings. Please see the inner exception for more details.", e);
            }
            #region Refresh timer
            try
            {
                int intervalInMinutes = DefaultRefreshTimeoutIntervalInMinutes;
                try
                {
                    XmlNode timeoutNode = doc.DocumentElement.Attributes["timeout"];
                    if (null != timeoutNode)
                    {
                        intervalInMinutes = int.Parse(timeoutNode.Value);
                    }
                    else
                    {
                        message = string.Format("The timeout node is missing. Using default value: {0} minutes.", DefaultRefreshTimeoutIntervalInMinutes);
                        Debug.WriteLine(message);
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"Cannot parse the timeout node. Ensure that the value is an integer (details follow): {e.ToString()}");
                }
                if (0 == intervalInMinutes)
                {
                    intervalInMinutes = DefaultRefreshTimeoutIntervalInMinutes;
                    message = string.Format("The timeout cannot be set to 0. Using default value: {0} minutes.", DefaultRefreshTimeoutIntervalInMinutes);
                    Debug.WriteLine(message);
                }
                timeoutTimer = new Timer(intervalInMinutes * 1000 * 60);
                timeoutTimer.Elapsed += timeoutTimer_Elapsed;
                timeoutTimer.Start();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                throw new SettingsException("Unexpected error while setting up the refresh timer. Please see the inner exception for more details.", e);
            }
            decimal elapsedTimeInSeconds = (DateTime.Now.Ticks - ticks) / 10000000;
            message = string.Format("Application settings data loaded in {0} seconds.", elapsedTimeInSeconds);
            Debug.WriteLine(message);
            if (elapsedTimeInSeconds > 0.1M)
            {
                Debug.WriteLine(string.Format("Application settings data loaded unexpectedly slowly: {0} seconds.", elapsedTimeInSeconds));
            } 
            #endregion
        }
private static void LoadSettings ( ) { string message = " " ; Debug . WriteLine ( message ) ; long ticks = DateTime . Now . Ticks ; XmlDocument doc = null ; region  Validation: Throw an exception is settings files cannot be found.  string defaultPath = ConfigurationManager . AppSettings [ AppSettingsPathKeyName ] ; if ( String . IsNullOrEmpty ( defaultPath ) ) throw new SettingsException ( $" Missing key ' { AppSettingsPathKeyName } ' in the appSettings of the default configuration file. " , null ) ; if ( ! Path . IsPathRooted ( defaultPath ) ) { defaultPath = Path . GetFullPath ( defaultPath ) ; } if ( ! System . IO . Directory . Exists ( defaultPath ) ) { throw new SettingsException ( $" Missing configuration folder. The folder ' { defaultPath } ' in key ' { AppSettingsPathKeyName } ' in the appSettings of the configuration file does not exist. " , null ) ; } endregion try { settingsList = new List < SettingProperties > ( ) ; SettingsLoader loader = new SettingsLoader ( settingsList , SectionSeparator ) ; foreach ( string file in Directory . GetFiles ( defaultPath , " " ) ) { Console . WriteLine ( $" Loading application settings data on file:  { file } " ) ; loader . LoadSettings ( file ) ; Console . WriteLine ( $" Done loading application settings data on file:  { file } " ) ; } } catch ( Exception e ) { Debug . WriteLine ( e . ToString ( ) ) ; throw new SettingsException ( " " , e ) ; } region  Refresh timer try { int intervalInMinutes = DefaultRefreshTimeoutIntervalInMinutes ; try { XmlNode timeoutNode = doc . DocumentElement . Attributes [ " " ] ; if ( null != timeoutNode ) { intervalInMinutes = int . Parse ( timeoutNode . Value ) ; } else { message = string . Format ( " " , DefaultRefreshTimeoutIntervalInMinutes ) ; Debug . WriteLine ( message ) ; } } catch ( Exception e ) { Debug . WriteLine ( $" Cannot parse the timeout node. Ensure that the value is an integer (details follow):  { e . ToString ( ) } " ) ; } if ( 0 == intervalInMinutes ) { intervalInMinutes = DefaultRefreshTimeoutIntervalInMinutes ; message = string . Format ( " " , DefaultRefreshTimeoutIntervalInMinutes ) ; Debug . WriteLine ( message ) ; } timeoutTimer = new Timer ( intervalInMinutes * 1000 * 60 ) ; timeoutTimer . Elapsed += timeoutTimer_Elapsed ; timeoutTimer . Start ( ) ; } catch ( Exception e ) { Debug . WriteLine ( e . ToString ( ) ) ; throw new SettingsException ( " " , e ) ; } decimal elapsedTimeInSeconds = ( DateTime . Now . Ticks - ticks ) / 10000000 ; message = string . Format ( " " , elapsedTimeInSeconds ) ; Debug . WriteLine ( message ) ; if ( elapsedTimeInSeconds > 0.1M ) { Debug . WriteLine ( string . Format ( " " , elapsedTimeInSeconds ) ) ; } endregion }
Load all sections and settings in memory.
Load all sections and settings in memory .
//get the default path //if relative path, get full path // Load all settings files   // Load settings file // Read the timeout information, if any