Exemple #1
0
        /// <summary>
        /// Get a bool value from the configuration
        /// </summary>
        /// <param name="Category">The category</param>
        /// <param name="Key">The key</param>
        /// <param name="Default">The default value</param>
        /// <returns>The value if found, default if not</returns>
        public bool GetBool(ConfigurationSections Category, string Key, bool Default)
        {
            object value = GetValue(Category.ToString(), Key, Default);

            if (value.GetType() == typeof(Boolean))
            {
                return((bool)value);
            }
            else
            {
                return(Boolean.Parse(value.ToString()));
            }
        }
Exemple #2
0
        /// <summary>
        /// Get a int value from the configuration
        /// </summary>
        /// <param name="Category">The category</param>
        /// <param name="Key">The key</param>
        /// <param name="Default">The default value</param>
        /// <returns>The value if found, default if not</returns>
        public int GetInt(ConfigurationSections Category, string Key, int Default)
        {
            object value = GetValue(Category.ToString(), Key, Default);

            if (value.GetType() == typeof(Int32))
            {
                return((int)value);
            }
            else
            {
                return(Int32.Parse(value.ToString()));
            }
        }
Exemple #3
0
        /// <summary>
        /// Get a type value from the configuration
        /// </summary>
        /// <param name="Category">The category</param>
        /// <param name="Key">The key</param>
        /// <param name="Default">The default value</param>
        /// <returns>The value if found, default if not</returns>
        public Type GetType(ConfigurationSections Category, string Key, Type Default)
        {
            object value = GetValue(Category.ToString(), Key, Default);

            if (value == null)
            {
                return(null);
            }
            Type returnType = Type.GetType(value.ToString());

            if (returnType != null)
            {
                return(returnType);
            }
            return(Default);
        }
Exemple #4
0
        private ConfigurationSection CreateSection(string sectionPath, string locationPath, FileContext core)
        {
            if (Location == null || Location == locationPath || locationPath.StartsWith(Location + '/'))
            {
                var definition = DefinitionCache.FirstOrDefault(item => item.Path == sectionPath);
                if (definition == null)
                {
                }
                else if (definition.Schema == null)
                {
                    if (!core.IgnoreSchemaCheck)
                    {
                        throw new FileNotFoundException($"Filename: \\\\?\\{core.FileName}\r\nError: The configuration section '{sectionPath}' cannot be read because it is missing schema\r\n\r\n");
                    }
                }
                else
                {
                    var section = new ConfigurationSection(
                        sectionPath,
                        definition.Schema.Root,
                        locationPath,
                        core,
                        null)
                    {
                        OverrideMode = OverrideMode.Inherit
                    };
                    if (locationPath == null)
                    {
                        section.OverrideModeEffective = (OverrideMode)Enum.Parse(typeof(OverrideMode), definition.OverrideModeDefault);
                    }
                    else
                    {
                        var parent = FindSection(sectionPath, locationPath.GetParentLocation(), core);
                        section.OverrideModeEffective = parent.OverrideModeEffective;
                    }

                    section.IsLocked        = section.FileContext.FileName != definition.FileContext.FileName && section.OverrideModeEffective != OverrideMode.Allow;
                    section.IsLocallyStored = section.FileContext.FileName == definition.FileContext.FileName;
                    ConfigurationSections.Add(section);
                    return(section);
                }
            }

            var sectionBasedOnParent = core.Parent?.CreateSection(sectionPath, locationPath, core.Parent);

            return(sectionBasedOnParent);
        }
Exemple #5
0
        /// <summary>
        /// Get an enum value from the configuration
        /// </summary>
        /// <param name="Category">The category</param>
        /// <param name="Key">The key</param>
        /// <param name="Default">The default value</param>
        /// <typeparam name="T">The enum type</typeparam>
        /// <returns>The value if found, default if not</returns>
        public T GetEnum <T>(ConfigurationSections Category, string Key, T Default) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                return(Default);
            }

            string ValueAsString = GetString(Category, Key, "");

            if (String.IsNullOrWhiteSpace(ValueAsString))
            {
                return(Default);
            }

            foreach (T item in Enum.GetValues(typeof(T)))
            {
                if (item.ToString().ToLower().Equals(ValueAsString.Trim().ToLower()))
                {
                    return(item);
                }
            }

            return(Default);
        }
Exemple #6
0
        /// <summary>
        /// Checks if the configuration has set a value for the given category and key
        /// </summary>
        /// <param name="Category">The category</param>
        /// <param name="Key">The key</param>
        /// <returns>True if the config has a value</returns>
        public bool HasValue(ConfigurationSections Category, string Key)
        {
            string CatKey = Category.ToString();

            return(Configuration.ContainsKey(CatKey) && Configuration[CatKey].ContainsKey(Key));
        }
Exemple #7
0
 /// <summary>
 /// Get a string value from the configuration
 /// </summary>
 /// <param name="Category">The category</param>
 /// <param name="Key">The key</param>
 /// <param name="Default">The default value</param>
 /// <returns>The value if found, default if not</returns>
 public string GetString(ConfigurationSections Category, string Key, string Default)
 {
     return(GetValue(Category.ToString(), Key, Default).ToString());
 }
        /// <summary>
        /// Gets the configuration for the corresponding configuration section.
        /// </summary>
        public string GetConfiguration(ConfigurationSections configSection)
        {
            XDocument xDocument = null;

            if (this.Configuration != null)
            {
                XElement root          = this.Configuration.Root;
                XElement xElement      = root.Element(AppInsightsConfigReader.AINamespace + "ActiveProfile");
                XElement xElement2     = root.Element(AppInsightsConfigReader.AINamespace + "Profiles");
                string   activeProfile = (xElement != null) ? xElement.Value : null;
                if (xElement2 != null && activeProfile != null)
                {
                    XElement xElement3 = xElement2.Element(AppInsightsConfigReader.AINamespace + "Defaults");
                    xElement = (
                        from e in xElement2.Elements(AppInsightsConfigReader.AINamespace + "Profile")
                        where e.Attribute("name") != null && string.Equals(e.Attribute("name").Value, activeProfile)
                        select e).FirstOrDefault <XElement>();
                    if (xElement != null)
                    {
                        xDocument = new XDocument(new object[]
                        {
                            new XElement(AppInsightsConfigReader.AINamespace + "ProfileSettings", (xElement3 != null) ? xElement3.Elements() : null)
                        });
                        xDocument = AppInsightsConfigReader.MergeActiveProfile(xDocument, xElement);
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.ServerAnalytics))
                        {
                            XElement xElement4 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "ServerAnalytics").FirstOrDefault <XElement>();
                            if (xElement4 != null)
                            {
                                xElement4.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.ClientAnalytics))
                        {
                            XElement xElement5 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "ClientAnalytics").FirstOrDefault <XElement>();
                            if (xElement5 != null)
                            {
                                xElement5.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.MMAOutputChannels))
                        {
                            XElement xElement6 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "MMAOutputChannels").FirstOrDefault <XElement>();
                            if (xElement6 != null)
                            {
                                xElement6.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.APMSettings))
                        {
                            XElement xElement7 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "APMSettings").FirstOrDefault <XElement>();
                            if (xElement7 != null)
                            {
                                xElement7.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.MemoryEventSettings))
                        {
                            XElement xElement8 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "MemoryEventSettings").FirstOrDefault <XElement>();
                            if (xElement8 != null)
                            {
                                xElement8.Remove();
                            }
                        }
                        if (!AppInsightsConfigReader.IsFlagSet(configSection, ConfigurationSections.PerformanceCounters))
                        {
                            XElement xElement9 = xDocument.Descendants(AppInsightsConfigReader.AINamespace + "PerformanceCounters").FirstOrDefault <XElement>();
                            if (xElement9 != null)
                            {
                                xElement9.Remove();
                            }
                        }
                    }
                }
            }
            if (xDocument == null)
            {
                return(null);
            }
            return(xDocument.ToString());
        }
 private static bool IsFlagSet(ConfigurationSections flags, ConfigurationSections flag)
 {
     return((flags & flag) == flag);
 }