Пример #1
0
        /// <summary>
        /// 开启对某个配置文件的监控
        /// </summary>
        /// <param name="filePath">文件路径</param>
        private static void InitMoniter(string filePath)
        {
            try
            {
                var fileWatcher = new FileSystemWatcher(System.IO.Path.GetDirectoryName(filePath), System.IO.Path.GetFileName(filePath));
                fileWatcher.IncludeSubdirectories = false;
                fileWatcher.Changed += (sender, e) =>
                {
                    var    cgFile = e.FullPath;
                    object oldCfg = GetExists(cgFile);
                    if (oldCfg == null)
                    {
                        ConfigLogger.CacheIsNotFind(cgFile);
                        return;
                    }

                    var type = oldCfg.GetType();

                    ConfigRefQueue.Instance.Enqueue(new ConfigRefItem()
                    {
                        ConfigEntityType = type,
                        AddTime          = DateTime.Now,
                        FilePath         = cgFile
                    });
                };

                fileWatcher.EnableRaisingEvents = true;
            }
            catch (Exception ex)
            {
                ConfigLogger.UnKnowError(ex);
            }
        }
Пример #2
0
        /// <summary>
        /// 设置配置文件实体
        /// </summary>
        /// <param name="cfgKey">配置文件缓存Key值</param>
        /// <param name="config">配置实体</param>
        internal static void Set(string cfgKey, object config)
        {
            try
            {
                _lock.EnterWriteLock();
                if (_configs.ContainsKey(cfgKey))
                {
                    _configs[cfgKey] = config;
                }
                else
                {
                    _configs.Add(cfgKey, config);

                    InitMoniter(cfgKey);
                }
                ConfigLogger.LoadConfigSucess(cfgKey);
            }
            catch (Exception ex)
            {
                ConfigLogger.UnKnowError(ex);
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
Пример #3
0
        /// <summary>
        /// 加载配置实体
        /// </summary>
        /// <param name="filePath">配置文件完整路径</param>
        /// <param name="cfgType"></param>
        /// <returns>返回加载是否成功</returns>
        internal static object LoadConfig(string filePath, Type cfgType)
        {
            if (!File.Exists(filePath))
            {
                ConfigLogger.FileIsNotFind(filePath);
                return(null);
            }
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // 使用 IConfiguration 接口填充配置实体
                    var parser = cfgType.Assembly.CreateInstance(cfgType.FullName) as IConfiguration;
                    if (parser != null)
                    {
                        try
                        {
                            parser.Fill(fs);
                            return(parser);
                        }
                        catch (Exception ex)
                        {
                            ConfigLogger.IFileConfigError(cfgType, ex);
                            return(null);
                        }
                    }
                    // 使用XML序列化实体
                    else
                    {
                        var fileContent = fs.ReadToEndAsync(_encode).ConfigureAwait(false).GetAwaiter().GetResult();
                        return(SnailCore.Data.Serializer.XmlDeserialize(fileContent, cfgType));
                    }
                }
            }
            catch (System.IO.IOException ioEx)
            {
                ConfigLogger.ReadFileError(filePath, ioEx);

                return(null);
            }
            catch (Exception ex)
            {
                ConfigLogger.UnKnowError(ex);

                return(null);
            }
            return(null);
        }