void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Cache) { this.cacheFactory = new CacheFactory(configService); } }
private static void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.AppSettings) { // 配置文件变化,只影响当前的运行模式 local.runMode = null; } }
void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Services) { try { ManageableServiceHost[] newHosts = this.CreateServiceHosts(); // 异步关闭当前宿主 if (this.hosts != null) { for (int i = 0; i < this.hosts.Length; i++) { try { this.hosts[i].Close(); } catch { try { this.hosts[i].Abort(); } catch { } } } } this.hosts = newHosts; // 打开新的宿主 for (int i = 0; i < this.hosts.Length; i++) { try { this.hosts[i].Open(); } catch (Exception err) { try { this.hosts[i].Abort(); } catch { } this.LogService.Error(err); } } } catch (Exception err) { this.LogService.Error(err); } } }
private void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Log) { log4net.Repository.ILoggerRepository oldRepository = this.repository; ICustomLog oldLog4release = this.logger4release; ICustomLog oldLog4demo = this.logger4demo; log4net.Repository.ILoggerRepository newRepository = null; ICustomLog newLog4release = null; ICustomLog newLog4demo = null; lock (syncForRepository) { this.repository = null; this.logger4release = null; this.logger4demo = null; // 输出所有当前未输出日志然后关闭当前日志 try { log4net.Appender.IAppender[] oldAppenders = oldRepository.GetAppenders(); if (oldAppenders != null && oldAppenders.Length > 0) { for (int i = 0; i < oldAppenders.Length; i++) { if (oldAppenders[i] is log4net.Appender.BufferingAppenderSkeleton) { ((log4net.Appender.BufferingAppenderSkeleton)oldAppenders[i]).Flush(); } } } } catch{ } finally { oldRepository.Shutdown(); } // 创建新日志 try { StringBuilder sb = new StringBuilder(); newRepository = CustomLogManager.CreateRepository(Guid.NewGuid().ToString()); log4net.Config.XmlConfigurator.Configure(newRepository, new FileInfo(AppDomain.CurrentDomain.MapPhysicalPath("conf\\log.config"))); newLog4release = CustomLogManager.GetLogger(newRepository, "release"); newLog4demo = CustomLogManager.GetLogger(newRepository, "demo"); FormatConfigurationErrorMessages(sb, newRepository.ConfigurationMessages); if (sb.Length > 0) //出错时 { sb.Insert(0, "在响应日志配置文件变化的过程中发生错误,仍将使用距变化发生时最近一次正确的配置。"); // 输出 errorMessages LogUtil.LogToErrorLog(sb.ToString(), log4net.Core.Level.Error, LogCategory.Configuration, null); // 继续使用现有日志 this.repository = null; this.logger4release = null; this.logger4demo = null; } else// 成功应用新配置时 { // 应用新日志 this.lastSuccessConfig = File.ReadAllText(AppDomain.CurrentDomain.MapPhysicalPath("conf\\log.config")); this.repository = newRepository; this.logger4release = newLog4release; this.logger4demo = newLog4demo; this.lock4loggers.EnterWriteLock(); try { this.loggers.Clear(); } finally { this.lock4loggers.ExitWriteLock(); } } } catch (Exception err) { XMS.Core.Container.LogService.Warn("在响应日志配置文件变化的过程中发生错误,仍将使用距变化发生时最近一次正确的配置。", Logging.LogCategory.Configuration, err); if (newRepository != null) { try { newRepository.Shutdown(); } catch { } } } finally { // 更新未成功,使用上次正确的日志配置重新初始化日志 if (this.repository == null) { if(this.lastSuccessConfig != null) { this.repository = CustomLogManager.CreateRepository(Guid.NewGuid().ToString()); using(MemoryStream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(this.lastSuccessConfig))) { log4net.Config.XmlConfigurator.Configure(this.repository, stream); } this.logger4release = CustomLogManager.GetLogger(this.repository, "release"); this.logger4demo = CustomLogManager.GetLogger(this.repository, "demo"); } else { this.repository = oldRepository; this.logger4release = oldLog4release; this.logger4demo = oldLog4demo; } } } } } }
private void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Cache) { CacheSettings oldSettings = instance; // 临时保存当前正确的实例 // 在配置文件变化时, 结合 CacheSettings.Instance 单例模式,通过将实例对象设置为 null,暂时阻止对单例实例的访问 lock (syncForSettings) { instance = null; // 将 instance 设置为 null,暂时阻止对单例实例的访问 CacheSettings newSettings = null; try { newSettings = new CacheSettings(); instance = newSettings; // 启用新的配置 } catch (Exception err) { instance = oldSettings; // 恢复为最近一次正确的配置 XMS.Core.Container.LogService.Warn("在响应配置文件变化的过程中发生错误,仍将使用距变化发生时最近一次正确的配置。", Logging.LogCategory.Configuration, err); } // 成功应用新的配置后,处理配置文件变化监听事件 if (instance != oldSettings) { if (oldSettings != null)// 为旧实例移除配置变化监听事件 { // 这里不能通过 Dispose 释放事件,因为 oldSettings 的实例有可能被在缓存相关操作方法里通过局部变量引用,后面仍然有可能继续使用 // 因此这里手动释放旧实例的监听事件 oldSettings.ConfigService.ConfigFileChanged -= oldSettings.configFileChangedEventHandler; } } } // 单例实例发生变化后,在 lock (syncForSettings) 语句之外清空缓存,以避免与 Clear 过程中发生死锁(因为锁定过程中会访问 CacheSettings 的单例实例) if (instance != oldSettings) { LocalCacheManager.Instance.Clear(); } } }
private static void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileName.Equals("ErrorCodes.config", StringComparison.InvariantCultureIgnoreCase)) { try { Dictionary<string, StringTemplate> templates = new Dictionary<string, StringTemplate>(StringComparer.InvariantCultureIgnoreCase); ErrorCodesSection section = GetSection("errorCodes"); if (section != null) { foreach (ErrorCodeElement element in section.ErrorCodes) { templates.Add(element.Key, new StringTemplate(element.Message)); } } _templates = templates; } catch (Exception err) { XMS.Core.Container.LogService.Warn(String.Format("在响应配置文件{0}变化的过程中发生错误,仍将使用距变化发生时最近一次正确的配置。", e.ConfigFileName), Logging.LogCategory.Configuration, err); } } }
private static void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.ServiceReferences) { try { BindServiceConfiguration(configService, listServiceTypes, GetRetryingTimeInterval()); } catch (ConfigurationErrorsException confErr) { // 记录日志 XMS.Core.Container.LogService.Error("在响应服务引用配置文件变化的过程中发生错误,仍将使用距变化发生时最近一次正确的配置。", confErr); } catch (Exception err) { // 记录日志 XMS.Core.Container.LogService.Error(err); } } }
static void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Services) { try { ApplyConfigFileChange(); } catch (Exception err) { // 发生严重错误 LogService.Fatal(err); } } }
private void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { //if (e.ConfigFileType == ConfigFileType.AppSettings) //{ // lock (this.syncObject) // { // // 配置文件发生变化时 // // 先停止服务管理器 // this.Stop(); // //然后重新启动服务管理器 // this.Start(); // } //} }
private void configService_ConfigFileChanged(object sender, ConfigFileChangedEventArgs e) { if (e.ConfigFileType == ConfigFileType.Services) { lock (this.syncObject) { // 配置文件发生变化时 // 先停止服务管理器 this.Stop(); //然后重新启动服务管理器 this.Start(); } } }