Exemplo n.º 1
0
        /// <summary>
        /// set a setting
        /// </summary>
        public void SetSetting(string name, object value, string groupId = null)
        {
            string key = name;

            if (!string.IsNullOrEmpty(groupId))
            {
                key = $"{groupId}.{name}";
            }
            string strValue = value?.ToString();

            if (SettingsDictionary.ContainsKey(key))
            {
                SettingsDictionary[key] = strValue;
            }
            else
            {
                SettingsDictionary.Add(key, strValue);
            }
            SetGroupSetting(groupId, name, value);

            if (AutoSave)
            {
                SaveSettings();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the values of the file into memory.
        /// </summary>
        private void LoadValuesFromFile()
        {
            if (!File.Exists(ConfigPath))
            {
                //if the config file is not where it's supposed to be create a new one.
                throw new Exception("Config file not found: " + ConfigPath);
            }

            //load the xml
            var configXml = XDocument.Load(ConfigPath);

            //get all of the <setting name="..." serializeAs="..."> elements.
            var settingElements = configXml.Element(CONFIG).Element(APPLICATION_SETTINGS).Element(typeof(Properties_Settings).FullName).Elements(SETTING);

            //iterate through, adding them to the dictionary, (checking for nulls, xml no likey nulls)
            //using "String" as default serializeAs...just in case, no real good reason.
            foreach (var element in settingElements)
            {
                var newSetting = new SettingStruct()
                {
                    name        = element.Attribute(NAME) == null ? String.Empty : element.Attribute(NAME).Value,
                    serializeAs = element.Attribute(SERIALIZE_AS) == null ? "String" : element.Attribute(SERIALIZE_AS).Value,
                    value       = element.Value ?? String.Empty
                };

                if (newSetting.serializeAs == "Xml")
                {
                    var e = (XElement)element.Nodes().First();
                    newSetting.value = e.LastNode.ToString() ?? String.Empty;
                }
                ;

                SettingsDictionary.Add(element.Attribute(NAME).Value, newSetting);
            }
        }
Exemplo n.º 3
0
        public override void EventProcessing(EventContext context) {
            if (!context.Event.IsError())
                return;

            Error error = context.Event.GetError();
            if (error == null)
                return;

            if (String.IsNullOrWhiteSpace(context.Event.Message))
                context.Event.Message = error.Message;

            string[] commonUserMethods = { "DataContext.SubmitChanges", "Entities.SaveChanges" };
            if (context.HasProperty("CommonMethods"))
                commonUserMethods = context.GetProperty<string>("CommonMethods").SplitAndTrim(',');

            string[] userNamespaces = null;
            if (context.HasProperty("UserNamespaces"))
                userNamespaces = context.GetProperty<string>("UserNamespaces").SplitAndTrim(',');

            var signature = new ErrorSignature(error, userCommonMethods: commonUserMethods, userNamespaces: userNamespaces);
            if (signature.SignatureInfo.Count <= 0)
                return;

            var targetInfo = new SettingsDictionary(signature.SignatureInfo);
            var stackingTarget = error.GetStackingTarget();
            if (stackingTarget != null && stackingTarget.Error != null)
                targetInfo.Add("Message", error.GetStackingTarget().Error.Message);

            error.Data[Error.KnownDataKeys.TargetInfo] = targetInfo;

            foreach (var key in signature.SignatureInfo.Keys)
                context.StackSignatureData.Add(key, signature.SignatureInfo[key]);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Must override this, this is the bit that does the saving to file.  Called when Settings.Save() is called
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collection"></param>
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            //grab the values from the collection parameter and update the values in our dictionary.
            foreach (SettingsPropertyValue value in collection)
            {
                var setting = new SettingStruct()
                {
                    value       = (value.PropertyValue == null ? String.Empty : value.SerializedValue.ToString()),
                    name        = value.Name,
                    serializeAs = value.Property.SerializeAs.ToString()
                };

                if (!SettingsDictionary.ContainsKey(value.Name))
                {
                    SettingsDictionary.Add(value.Name, setting);
                }
                else
                {
                    SettingsDictionary[value.Name] = setting;
                }
            }

            //now that our local dictionary is up-to-date, save it to disk.
            SaveValuesToFile();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the values of the file into memory.
        /// </summary>
        private void LoadValuesFromFile()
        {
            if (!File.Exists(UserConfigPath))
            {
                //if the config file is not where it's supposed to be create a new one.
                CreateEmptyConfig();
            }

            //load the xml
            var configXml = XDocument.Load(UserConfigPath);

            //get all of the <setting name="..." serializeAs="..."> elements.
            var settingElements = configXml.Element(CONFIG).Element(USER_SETTINGS).Element(typeof(Properties.Settings).FullName).Elements(SETTING);

            //iterate through, adding them to the dictionary, (checking for nulls, xml no likey nulls)
            //using "String" as default serializeAs...just in case, no real good reason.
            foreach (var element in settingElements)
            {
                var newSetting = new SettingStruct()
                {
                    name        = element.Attribute(NAME) == null ? String.Empty : element.Attribute(NAME).Value,
                    serializeAs = element.Attribute(SERIALIZE_AS) == null ? "String" : element.Attribute(SERIALIZE_AS).Value,
                    value       = element.Value ?? String.Empty
                };
                SettingsDictionary.Add(element.Attribute(NAME).Value, newSetting);
            }
        }
Exemplo n.º 6
0
        private static SettingsDictionary GetSettings(IDictionary <string, string> values)
        {
            SettingsDictionary settings = new SettingsDictionary();

            settings.Connections.Add("name", new ConnectionSettings("name", "connectionstring"));
            foreach (KeyValuePair <string, string> value in values)
            {
                settings.Add(value.Key, value.Value);
            }
            return(settings);
        }
        public void RemapIntToEnumName_KeyExists_ValueIsLong_ValueIsNotValid_ReturnsCorrectEnum()
        {
            SettingsDictionary settings = new SettingsDictionary();

            settings.Add(Key1, LongValue);

            settings.RemapIntToEnumName <TestEnum>(Key1);

            Assert.AreEqual(1, settings.Count);
            Assert.AreEqual(LongValue, settings[Key1]);
        }
Exemplo n.º 8
0
        public void CanGetLogSettingsMultithreaded()
        {
            var settings = new SettingsDictionary();

            settings.Add("@@log:*", "Info");
            settings.Add("@@log:Source1", "Trace");
            settings.Add("@@log:Source2", "Debug");
            settings.Add("@@log:Source3", "Info");
            settings.Add("@@log:Source4", "Info");

            var result = Parallel.For(0, 100, index => {
                var level = settings.GetMinLogLevel("Source1");
                _writer.WriteLine("Source1 log level: {0}", level);
            });

            while (!result.IsCompleted)
            {
                Thread.Sleep(1);
            }
        }
Exemplo n.º 9
0
        public override void EventProcessing(EventContext context)
        {
            if (!context.Event.IsError())
            {
                return;
            }

            Error error = context.Event.GetError();

            if (error == null)
            {
                return;
            }

            if (String.IsNullOrWhiteSpace(context.Event.Message))
            {
                context.Event.Message = error.Message;
            }

            string[] commonUserMethods = { "DataContext.SubmitChanges", "Entities.SaveChanges" };
            if (context.HasProperty("CommonMethods"))
            {
                commonUserMethods = context.GetProperty <string>("CommonMethods").SplitAndTrim(',');
            }

            string[] userNamespaces = null;
            if (context.HasProperty("UserNamespaces"))
            {
                userNamespaces = context.GetProperty <string>("UserNamespaces").SplitAndTrim(',');
            }

            var signature = new ErrorSignature(error, userCommonMethods: commonUserMethods, userNamespaces: userNamespaces);

            if (signature.SignatureInfo.Count <= 0)
            {
                return;
            }

            var targetInfo     = new SettingsDictionary(signature.SignatureInfo);
            var stackingTarget = error.GetStackingTarget();

            if (stackingTarget != null && stackingTarget.Error != null)
            {
                targetInfo.Add("Message", error.GetStackingTarget().Error.Message);
            }

            error.Data[Error.KnownDataKeys.TargetInfo] = targetInfo;

            foreach (var key in signature.SignatureInfo.Keys)
            {
                context.StackSignatureData.Add(key, signature.SignatureInfo[key]);
            }
        }
        public void SettingsAreCaseSensitive()
        {
            SettingsDictionary settings = new SettingsDictionary
            {
                { Key1, StringValue }
            };

            Assert.IsFalse(settings.TryGetValue(Key1LowerCase, out object value));
            settings.Add(Key1LowerCase, LongValue);

            Assert.AreEqual(2, settings.Count);
            Assert.AreEqual(StringValue, settings[Key1]);
            Assert.AreEqual(LongValue, settings[Key1LowerCase]);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Selects a different group of settings. If the group does not exist, it is created.
        /// </summary>
        /// <param name="group">The group of settings to select.</param>
        public void SelectGroup(string group)
        {
            Dictionary <string, string> table;

            CurrentGroup = group;

            if (SettingsDictionary.TryGetValue(group, out table))
            {
                return;
            }


            SettingsDictionary.Add(group, new Dictionary <string, string>());
        }
Exemplo n.º 12
0
        static SettingsDictionary ConvertToDictionary(string settings)
        {
            var result = new SettingsDictionary();

            foreach (string item in settings.Split(';'))
            {
                string[] keyAndValue = item.Split('=');
                if (keyAndValue.Length == 2)
                {
                    result.Add(keyAndValue[0], keyAndValue[1]);
                }
            }

            return(result);
        }
Exemplo n.º 13
0
        public void CanGetSettingsMultithreaded()
        {
            var settings = new SettingsDictionary();
            var result   = Parallel.For(0, 20, index => {
                for (int i = 0; i < 10; i++)
                {
                    string key = $"setting-{i}";
                    if (!settings.ContainsKey(key))
                    {
                        settings.Add(key, (index * i).ToString());
                    }
                    else
                    {
                        settings[key] = (index * i).ToString();
                    }
                }
            });

            while (!result.IsCompleted)
            {
                Thread.Yield();
            }
        }
        /// <summary>
        /// Must override this, this is the bit that does the saving to file.  Called when Settings.Save() is called
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collection"></param>
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            //grab the values from the collection parameter and update the values in our dictionary.
            foreach (SettingsPropertyValue value in collection)
            {
                try
                {
                    Type t = GetType(value.Property.PropertyType.FullName);

                    var setting = new SettingStruct()
                    {
                        value = (value.PropertyValue == null ? String.Empty
                                 //: value.PropertyValue.ToString()),
                            : (string)(TypeDescriptor.GetConverter(t).ConvertToInvariantString(value.PropertyValue))),

                        name        = value.Name,
                        serializeAs = value.Property.SerializeAs.ToString()
                    };

                    if (!SettingsDictionary.ContainsKey(value.Name))
                    {
                        SettingsDictionary.Add(value.Name, setting);
                    }
                    else
                    {
                        SettingsDictionary[value.Name] = setting;
                    }
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.Message);
                }
            }

            //now that our local dictionary is up-to-date, save it to disk.
            SaveValuesToFile();
        }
Exemplo n.º 15
0
 internal void EncodeSettings(SettingsProfile profile, SettingsDictionary settingsDictionary)
 {
     foreach (var entry in profile.Settings.Values)
     {
         try
         {
             // Find key
             SettingsKey key;
             settingsKeys.TryGetValue(entry.Name, out key);
             settingsDictionary.Add(entry.Name, entry.GetSerializableValue(key));
         }
         catch (Exception)
         {
         }
     }
 }
Exemplo n.º 16
0
 public void Add(string key, object value)
 {
     SettingsDictionary.Add(key, value);
 }
Exemplo n.º 17
0
        /// <summary>
        /// Stub function, this does the actual file loading. Run LoadFile(), not this function directly.
        /// </summary>
        private void LoadFile_()
        {
            CurrentGroup = "";
            SettingsDictionary.Clear();

            using (var sr = new StreamReader(Filename)) {
                while (!sr.EndOfStream)
                {
                    var thisLine = sr.ReadLine();

                    if (thisLine == null || thisLine.StartsWith(";")) // -- Comment
                    {
                        continue;
                    }

                    if (thisLine.Contains(";")) // -- Deal with mid-line comments.
                    {
                        thisLine = thisLine.Substring(0, thisLine.IndexOf(";"));
                    }

                    if (!thisLine.Contains("=") && (!thisLine.Contains("[") && !thisLine.Contains("]")))
                    {
                        continue;
                    }

                    if ((thisLine.StartsWith("[") && thisLine.EndsWith("]")))   // -- Group.
                    {
                        var grpName = thisLine.Substring(1, thisLine.Length - 2);
                        Dictionary <string, string> group;

                        while (SettingsDictionary.TryGetValue(grpName, out group))
                        {
                            grpName += "§";
                            continue;
                        }

                        SettingsDictionary.Add(grpName, new Dictionary <string, string>());
                        CurrentGroup = grpName;
                        continue;
                    }

                    if (!thisLine.Contains("="))
                    {
                        continue;
                    }

                    var key   = thisLine.Substring(0, thisLine.IndexOf("=")).Trim(' ');
                    var value =
                        thisLine.Substring(thisLine.IndexOf("=") + 1, thisLine.Length - (thisLine.IndexOf("=") + 1))
                        .Trim(' ');

                    // -- Setting.
                    if (CurrentGroup == "" && !SettingsDictionary.ContainsKey(""))
                    {
                        SettingsDictionary.Add("", new Dictionary <string, string>());
                    }

                    if (SettingsDictionary[CurrentGroup].ContainsKey(key))
                    {
                        SettingsDictionary[CurrentGroup][key] = value;
                    }
                    else
                    {
                        SettingsDictionary[CurrentGroup].Add(key, value);
                    }
                }
            }
        }
Exemplo n.º 18
0
 private static SettingsDictionary GetSettings(IDictionary<string, string> values)
 {
     SettingsDictionary settings = new SettingsDictionary();
     settings.Connections.Add("name", new ConnectionSettings("name", "connectionstring"));
     foreach (KeyValuePair<string, string> value in values)
     {
         settings.Add(value.Key, value.Value);
     }
     return settings;
 }
Exemplo n.º 19
0
 public void Add(KeyValuePair <string, object> item)
 {
     SettingsDictionary.Add(item);
 }