public virtual void Save(IChromelyConfiguration config)
        {
            try
            {
                if (_chromelyDynamic == null ||
                    _chromelyDynamic.Empty ||
                    _chromelyDynamic.Dictionary == null ||
                    _chromelyDynamic.Dictionary.Count == 0)
                {
                    return;
                }

                var appSettingsFile = DataPath;

                if (string.IsNullOrWhiteSpace(appSettingsFile))
                {
                    appSettingsFile = AppSettingInfo.GetSettingsFilePath(config.Platform, AppName, true);
                }

                if (appSettingsFile == null)
                {
                    return;
                }

                using (StreamWriter streamWriter = File.CreateText(appSettingsFile))
                {
                    try
                    {
                        var options = new JsonSerializerOptions();
                        options.ReadCommentHandling = JsonCommentHandling.Skip;
                        options.AllowTrailingCommas = true;

                        var jsonDic = JsonSerializer.Serialize(_chromelyDynamic.Dictionary, options);
                        streamWriter.Write(jsonDic);

                        Logger.Instance.Log.LogInformation("AppSettings FileName:" + appSettingsFile);
                    }
                    catch (Exception exception)
                    {
                        Logger.Instance.Log.LogWarning(exception.ToString());
                        Logger.Instance.Log.LogWarning("If this is about cycle was detecttion please see - https://github.com/dotnet/corefx/issues/41288");
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Log.LogError(exception, exception.Message);
            }
        }
 private static void DeleteConfigFile(IChromelyConfiguration config)
 {
     try
     {
         var appSettingsFile = AppSettingInfo.GetSettingsFilePath(config.Platform, config.AppName ?? "chromely");
         if (!string.IsNullOrWhiteSpace(appSettingsFile))
         {
             if (File.Exists(appSettingsFile))
             {
                 File.Delete(appSettingsFile);
             }
         }
     }
     catch { }
 }
Beispiel #3
0
        public virtual void Save(IChromelyConfiguration config)
        {
            try
            {
                if (_chromelyDynamic == null || _chromelyDynamic.Empty)
                {
                    return;
                }

                var appSettingsFile = DataPath;

                if (string.IsNullOrWhiteSpace(appSettingsFile))
                {
                    appSettingsFile = AppSettingInfo.GetSettingsFilePath(config.Platform, AppName, true);
                }

                if (appSettingsFile == null)
                {
                    return;
                }

                using (StreamWriter streamWriter = File.CreateText(appSettingsFile))
                {
                    var options = new JsonSerializerOptions();
                    options.ReadCommentHandling = JsonCommentHandling.Skip;
                    options.AllowTrailingCommas = true;

                    var jsonDic = JsonSerializer.Serialize(_chromelyDynamic.Dictionary, options);
                    streamWriter.Write(jsonDic);

                    Logger.Instance.Log.Info("AppSettings FileName:" + appSettingsFile);
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Log.Error(exception);
            }
        }
        public virtual void Read(IChromelyConfiguration config)
        {
            try
            {
                var appSettingsFile = AppSettingInfo.GetSettingsFilePath(config.Platform, AppName);

                if (appSettingsFile == null)
                {
                    return;
                }

                var info = new FileInfo(appSettingsFile);
                if ((info.Exists) && info.Length > 0)
                {
                    using (StreamReader jsonReader = new StreamReader(appSettingsFile))
                    {
                        string json    = jsonReader.ReadToEnd();
                        var    options = new JsonSerializerOptions();
                        options.ReadCommentHandling = JsonCommentHandling.Skip;
                        options.AllowTrailingCommas = true;

                        var settingsJsonElement = JsonSerializer.Deserialize <JsonElement>(json, options);
                        var settingsDic         = JsonToDictionary(settingsJsonElement);
                        _chromelyDynamic = new ChromelyDynamic(settingsDic);
                    }
                }

                if (File.Exists(appSettingsFile))
                {
                    DataPath = appSettingsFile;
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Log.LogError(exception, exception.Message);
            }
        }