Пример #1
0
        public void Reload()
        {
            var     errors        = new List <ValidationResult>();
            JObject config        = null;
            object  updatedConfig = null;

            try
            {
                config = ConfigCache.CreateJsonConfig(ConfigPath) ?? Empty;

                if (JToken.DeepEquals(LatestNode, config))
                {
                    if (Latest != null)
                    {
                        ValidationErrors = null;
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                errors.Add(new ValidationResult("Failed to acquire config JObject: " + HealthMonitor.GetMessages(ex)));
            }

            if (config != null && errors.Any() == false)
            {
                LatestNode = config;

                try
                {
                    updatedConfig = LatestNode.ToObject(ObjectType);
                }
                catch (JsonException ex)
                {
                    errors.Add(new ValidationResult("Failed to deserialize config object: " + HealthMonitor.GetMessages(ex)));
                }

                if (updatedConfig != null)
                {
                    Validator.TryValidateObjectRecursive(updatedConfig, errors);
                }
            }

            if (errors.Any() == false)
            {
                Latest           = updatedConfig;
                ValidationErrors = null;
                UsageTracking.AddConfigObject(Latest, ConfigPath);

                Log.Info(_ => _("A config object has been updated", unencryptedTags: new
                {
                    ConfigObjectType = ObjectType.FullName,
                    ConfigObjectPath = ConfigPath
                }));

                SendChangeNotification?.Invoke(Latest);
            }
            else
            {
                ValidationErrors = string.Join(" \n", errors.Select(a => a.ErrorMessage));

                Log.Error(_ => _("A config object has been updated but failed validation", unencryptedTags: new
                {
                    ConfigObjectType = ObjectType.FullName,
                    ConfigObjectPath = ConfigPath,
                    ValidationErrors
                }));
            }
        }
Пример #2
0
        public void Reload()
        {
            var     errors        = new List <ValidationResult>();
            JObject config        = null;
            object  updatedConfig = null;

            try
            {
                config = ConfigCache.CreateJsonConfig(ConfigPath) ?? Empty;

                if (JToken.DeepEquals(LatestNode, config))
                {
                    if (Latest != null)
                    {
                        ValidationErrors = null;
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                errors.Add(new ValidationResult("Failed to acquire config JObject: " + HealthMonitor.GetMessages(ex)));
            }

            if (config != null && errors.Any() == false)
            {
                try
                {
                    updatedConfig = config.ToObject(ObjectType);
                }
                catch (Exception ex)
                {
                    // It is not only JsonException, as sometimes a custom deserializer capable to throw god knows what (including ProgrammaticException)
                    errors.Add(new ValidationResult("Failed to deserialize config object: " + HealthMonitor.GetMessages(ex)));
                }

                if (updatedConfig != null)
                {
                    Validator.TryValidateObjectRecursive(updatedConfig, errors);
                }
            }

            if (errors.Any() == false)
            {
                ValidationErrors = null;
                UsageTracking.AddConfigObject(Latest, ConfigPath);
                if (isCreated)
                {
                    Log.Info(_ => _("A config object has been updated",
                                    unencryptedTags: new {
                        ConfigObjectType  = ObjectType.FullName,
                        ConfigObjectPath  = ConfigPath,
                        OverallModifyTime = ConfigCache.LatestConfigFileModifyTime,
                    },
                                    encryptedTags: new {
                        Changes = DiffJObjects(LatestNode, config, new StringBuilder(), new Stack <string>()).ToString(),
                    }));
                }
                else//It mean we are first time not need to send update messsage
                {
                    isCreated = true;
                }

                LatestNode = config;
                Latest     = updatedConfig;
                SendChangeNotification?.Invoke(Latest);
            }
            else
            {
                ValidationErrors = string.Join(" \n", errors.Select(a => a.ErrorMessage));

                Log.Error(_ => _("A config object has been updated but failed validation", unencryptedTags: new
                {
                    ConfigObjectType = ObjectType.FullName,
                    ConfigObjectPath = ConfigPath,
                    ValidationErrors
                }));
            }
        }