internal static void CheckNChangeEnvironment() { bool envChanged = false; string environment = ChoAppFrxSettings.Me.AppEnvironment.NTrim(); string sharedEnvironmentConfigFilePath = ChoSharedEnvironmentManager.SharedEnvironmentConfigFilePath; string appFrxFilePath = ChoSharedEnvironmentManager.AppFrxFilePath; if (environment != Environment) { envChanged = true; ChoApplication.Trace(true, "Environment changed from '{0}' to '{1}'.".FormatString(Environment, environment)); Environment = environment; } if (sharedEnvironmentConfigFilePath != _sharedEnvironmentConfigFilePath) { envChanged = true; ChoApplication.Trace(true, "SharedEnvironmentConfgiFilePath changed from '{0}' to '{1}'.".FormatString(_sharedEnvironmentConfigFilePath, sharedEnvironmentConfigFilePath)); _sharedEnvironmentConfigFilePath = sharedEnvironmentConfigFilePath; } if (appFrxFilePath != _appFrxFilePath) { envChanged = true; ChoApplication.Trace(true, "AppFrxFilePath changed from '{0}' to '{1}'.".FormatString(_appFrxFilePath, appFrxFilePath)); _appFrxFilePath = appFrxFilePath; } if (envChanged) { LoadSharedEnvironmentManager(); OnEnvironmentChanged(); } }
private static XmlDocument LoadBackupSharedEnvironmentConfigFile(string backupSharedEnvironmentConfigFilePath) { XmlDocument doc = new XmlDocument(); if (!backupSharedEnvironmentConfigFilePath.IsNullOrEmpty() && File.Exists(backupSharedEnvironmentConfigFilePath)) { ChoApplication.Trace(true, "Loading backup shared environment config file: {0}".FormatString(backupSharedEnvironmentConfigFilePath)); doc.Load(backupSharedEnvironmentConfigFilePath); } else { ChoApplication.Trace(true, "No backup shared environment config file found."); } return(doc); }
static ChoEnvironmentSettings() { _appConfigFileWatcher.DoNotUseGlobalQueue = true; _appConfigFileWatcher.SetConfigurationChangedEventHandler("OnEnvironmentChanged", (sender, e) => { //ConfigurationManager.RefreshSection("appSettings"); ChoAppFrxSettings.RefreshSection(); bool envChanged = false; string environment = ChoAppFrxSettings.Me.AppEnvironment.NTrim(); string sharedEnvironmentConfigFilePath = SharedEnvironmentConfigFilePath; if (environment != Environment) { envChanged = true; ChoApplication.Trace(true, "Environment changed from '{0}' to '{1}'.".FormatString(Environment, environment)); Environment = environment; } if (sharedEnvironmentConfigFilePath != _sharedEnvironmentConfigFilePath) { envChanged = true; ChoApplication.Trace(true, "SharedEnvironmentConfgiFilePath changed from '{0}' to '{1}'.".FormatString(_sharedEnvironmentConfigFilePath, sharedEnvironmentConfigFilePath)); _sharedEnvironmentConfigFilePath = sharedEnvironmentConfigFilePath; } if (envChanged) { LoadSharedEnvironmentManager(); OnEnvironmentChanged(); } }); Environment = ChoAppFrxSettings.Me.AppEnvironment.NTrim(); _sharedEnvironmentConfigFilePath = SharedEnvironmentConfigFilePath; LoadSharedEnvironmentManager(); _appConfigFileWatcher.StartWatching(); }
private static void LoadFrxConfigPath() { ChoEnvironmentDetails environmentDetails = null; lock (_padLock) { environmentDetails = _sharedEnvironmentManager.GetEnvironmentDetails(); if (environmentDetails != null && environmentDetails.Freeze) { Environment = environmentDetails.Name; ChoApplication.Trace(true, "This host is Freezed to '{0}' environment.".FormatString(environmentDetails.Name)); } else { environmentDetails = _sharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(Environment); if (environmentDetails == null) { if (!Environment.IsNullOrWhiteSpace()) { ChoApplication.Trace(true, "The '{0}' environment found in the configuration file.".FormatString(Environment)); } environmentDetails = _sharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(); } } } if (environmentDetails != null) { Environment = environmentDetails.Name; ChoApplication.Trace(true, "Using the '{0}' environment.".FormatString(Environment)); _configFilePath = environmentDetails.AppFrxFilePath; } else { ChoApplication.Trace(true, "No Environment found for this host."); _configFilePath = null; } }
private void Load(string sharedEnvironmentConfigFilePath) { BaseAppConfigDirectory = null; DefaultEnvironment = null; EnvironmentDetails = null; string backupSharedEnvironmentConfigFilePath = null; XmlDocument doc = new XmlDocument(); if (!String.IsNullOrEmpty(sharedEnvironmentConfigFilePath) && File.Exists(sharedEnvironmentConfigFilePath)) { ChoApplication.Trace(true, "Using shared environment config file: {0}".FormatString(sharedEnvironmentConfigFilePath)); backupSharedEnvironmentConfigFilePath = "{0}.{1}".FormatString(sharedEnvironmentConfigFilePath, ChoReservedFileExt.Cho); try { if (File.Exists(backupSharedEnvironmentConfigFilePath)) { File.SetAttributes(backupSharedEnvironmentConfigFilePath, FileAttributes.Archive); } using (ChoXmlDocument xmlDoc = new ChoXmlDocument(sharedEnvironmentConfigFilePath)) { doc = xmlDoc.XmlDocument; } //doc.Load(sharedEnvironmentConfigFilePath); doc.Save(backupSharedEnvironmentConfigFilePath); if (File.Exists(backupSharedEnvironmentConfigFilePath)) { File.SetAttributes(backupSharedEnvironmentConfigFilePath, FileAttributes.Hidden); } } catch (Exception ex) { ChoApplication.Trace(true, "Error loading shared environment config file: {0}.".FormatString(sharedEnvironmentConfigFilePath)); ChoApplication.Trace(true, ex.ToString()); doc = LoadBackupSharedEnvironmentConfigFile(backupSharedEnvironmentConfigFilePath); } } else if (ChoApplication.GetSharedEnvironmentConfigXml != null) { string xml = ChoApplication.GetSharedEnvironmentConfigXml(); backupSharedEnvironmentConfigFilePath = "SharedEnvironments.{0}.{1}".FormatString(ChoReservedFileExt.Xml, ChoReservedFileExt.Cho); if (!xml.IsNullOrWhiteSpace()) { ChoApplication.Trace(true, "Using shared environment xml:"); ChoApplication.Trace(true, xml); try { doc.LoadXml(xml); doc.Save(backupSharedEnvironmentConfigFilePath); } catch (Exception ex) { ChoApplication.Trace(true, "Error loading shared environment config xml."); ChoApplication.Trace(true, ex.ToString()); doc = LoadBackupSharedEnvironmentConfigFile(backupSharedEnvironmentConfigFilePath); } } } LoadXml(doc); }
public void Initialize() { if (ApplicationBehaviourSettings == null) { ApplicationBehaviourSettings = new ChoApplicationBehaviourSettings(); } if (TrayApplicationBehaviourSettings == null) { TrayApplicationBehaviourSettings = new ChoTrayApplicationBehaviourSettings(); } if (LogSettings == null) { LogSettings = new ChoLogSettings(); } if (ApplicationName.IsNullOrWhiteSpace()) { try { ApplicationName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } catch (System.Security.SecurityException ex) { ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); } } if (ApplicationName.IsNullOrEmpty()) { ChoApplication.OnFatalApplicationException(101, "Missing ApplicationName."); } ApplicationNameWithoutExtension = Path.GetFileNameWithoutExtension(ApplicationName); if (EventLogSourceName.IsNullOrWhiteSpace()) { EventLogSourceName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } if (LogSettings.LogTimeStampFormat.IsNullOrWhiteSpace()) { LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } if (LogSettings.LogFileName.IsNullOrWhiteSpace()) { LogSettings.LogFileName = ChoPath.ChangeExtension(ApplicationName, ChoReservedFileExt.Log); } LogSettings.LogFileName = ChoPath.CleanFileName(LogSettings.LogFileName); if (Path.IsPathRooted(LogSettings.LogFileName)) { LogSettings.LogFileName = Path.GetFileName(LogSettings.LogFileName); } try { DateTime.Now.ToString(LogSettings.LogTimeStampFormat); } catch (Exception ex) { ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, "Invalid LogTimeStampFormat '{0}' configured.".FormatString(LogSettings.LogTimeStampFormat)); ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } try { string sharedEnvConfigDir = null; if (!AppFrxConfigFilePath.IsNullOrEmpty()) { sharedEnvConfigDir = Path.GetDirectoryName(AppFrxConfigFilePath); } if (AppConfigFilePath.IsNullOrWhiteSpace()) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml))); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml)); } } else { if (!Path.IsPathRooted(AppConfigFilePath)) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, AppConfigFilePath)); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, AppConfigFilePath); } } else { ApplicationConfigFilePath = AppFrxConfigFilePath; } } ApplicationConfigDirectory = Path.GetDirectoryName(ApplicationConfigFilePath); } catch (System.Security.SecurityException ex) { // This security exception will occur if the caller does not have // some undefined set of SecurityPermission flags. ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); } #region Get HostName // Get the DNS host name of the current machine try { // Lookup the host name if (HostName.IsNullOrWhiteSpace()) { HostName = System.Net.Dns.GetHostName(); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } // Get the NETBIOS machine name of the current machine if (HostName.IsNullOrWhiteSpace()) { try { HostName = Environment.MachineName; } catch (InvalidOperationException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the machine name // You must have Unrestricted EnvironmentPermission to access resource } } #endregion Get HostName #region Get IpAddresses try { IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); for (int i = 0; i < localIPs.Length; i++) { IPAddresses.Add(localIPs[i].ToString()); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } #endregion Get IpAddresses if (LogSettings.TraceLevel < 0) { LogSettings.TraceLevel = 4; } if (!LogSettings.LogFolder.IsNullOrWhiteSpace()) { ApplicationLogDirectory = ChoString.ExpandProperties(LogSettings.LogFolder, ChoEnvironmentVariablePropertyReplacer.Instance); } //else // ApplicationLogDirectory = Path.Combine(Path.GetDirectoryName(ChoGlobalApplicationSettings.SharedEnvConfigPath), ChoReservedDirectoryName.Logs); if (ApplicationLogDirectory.IsNullOrWhiteSpace()) { if (ChoApplication.AppEnvironment.IsNullOrWhiteSpace()) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs); } else { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs, ChoApplication.AppEnvironment); } } if (!Path.IsPathRooted(ApplicationLogDirectory)) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ApplicationLogDirectory); } if (ChoApplication.ApplicationMode == ChoApplicationMode.Service) { TrayApplicationBehaviourSettings.TurnOn = false; } }
internal void PostInitialize() { if (ApplicationName.IsNullOrEmpty()) { ChoEnvironment.Exit(101, "Missing ApplicationName."); } if (EventLogSourceName.IsNullOrWhiteSpace()) { EventLogSourceName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } if (LogSettings.LogTimeStampFormat.IsNullOrWhiteSpace()) { LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } if (LogSettings.LogFileName.IsNullOrWhiteSpace()) { LogSettings.LogFileName = ChoPath.ChangeExtension(ApplicationName, ChoReservedFileExt.Log); } LogSettings.LogFileName = ChoPath.CleanFileName(LogSettings.LogFileName); if (Path.IsPathRooted(LogSettings.LogFileName)) { LogSettings.LogFileName = Path.GetFileName(LogSettings.LogFileName); } try { DateTime.Now.ToString(LogSettings.LogTimeStampFormat); } catch (Exception ex) { ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, "Invalid LogTimeStampFormat '{0}' configured.".FormatString(LogSettings.LogTimeStampFormat)); ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, ex.ToString()); LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } try { string sharedEnvConfigDir = null; if (!AppFrxConfigFilePath.IsNullOrEmpty()) { sharedEnvConfigDir = Path.GetDirectoryName(ChoPath.GetFullPath(AppFrxConfigFilePath)); } if (AppConfigFilePath.IsNullOrWhiteSpace()) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml))); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml)); } } else { if (!Path.IsPathRooted(AppConfigFilePath)) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, AppConfigFilePath)); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, AppConfigFilePath); } } else { ApplicationConfigFilePath = AppConfigFilePath; } } ApplicationConfigDirectory = Path.GetDirectoryName(ChoPath.GetFullPath(ApplicationConfigFilePath)); AppFrxConfigFilePath = Path.Combine(ApplicationConfigDirectory, ChoReservedFileName.CoreFrxConfigFileName); } catch (System.Security.SecurityException ex) { // This security exception will occur if the caller does not have // some undefined set of SecurityPermission flags. ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, ex.ToString()); } if (!LogSettings.LogFolder.IsNullOrWhiteSpace()) { ApplicationLogDirectory = ChoString.ExpandProperties(LogSettings.LogFolder, ChoEnvironmentVariablePropertyReplacer.Instance); } if (ApplicationLogDirectory.IsNullOrWhiteSpace()) { if (ChoApplication.AppEnvironment.IsNullOrWhiteSpace()) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs); } else { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs, ChoApplication.AppEnvironment); } } if (!Path.IsPathRooted(ApplicationLogDirectory)) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ApplicationLogDirectory); } if (ChoApplication.ApplicationMode == ChoApplicationMode.Service) { TrayApplicationBehaviourSettings.TurnOn = false; } }
public void Initialize() { #region Get ApplicationName if (ApplicationName.IsNullOrWhiteSpace()) { try { ApplicationName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } catch (System.Security.SecurityException ex) { ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, ex.ToString()); } } #endregion Get ApplicationName #region Get HostName // Get the DNS host name of the current machine try { // Lookup the host name if (HostName.IsNullOrWhiteSpace()) { HostName = System.Net.Dns.GetHostName(); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } // Get the NETBIOS machine name of the current machine if (HostName.IsNullOrWhiteSpace()) { try { HostName = Environment.MachineName; } catch (InvalidOperationException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the machine name // You must have Unrestricted EnvironmentPermission to access resource } } #endregion Get HostName #region Get IpAddresses try { IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); for (int i = 0; i < localIPs.Length; i++) { IPAddresses.Add(localIPs[i].ToString()); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } #endregion Get IpAddresses if (ApplicationBehaviourSettings == null) { ApplicationBehaviourSettings = new ChoApplicationBehaviourSettings(); } if (TrayApplicationBehaviourSettings == null) { TrayApplicationBehaviourSettings = new ChoTrayApplicationBehaviourSettings(); } if (LogSettings == null) { LogSettings = new ChoLogSettings(); } ChoApplication.RaiseGlobalApplicationSettingsOverrides(this); Merge(ChoGlobalApplicationSettings.Default); PostInitialize(); }
internal static ChoEnvironmentDetails GetEnvironmentDetails() { ChoEnvironmentDetails environmentDetails = null; lock (_padLock) { environmentDetails = SharedEnvironmentManager.GetEnvironmentDetails(); if ((environmentDetails != null) && environmentDetails.Freeze) { Environment = environmentDetails.Name; ChoApplication.Trace(true, "This host is Freezed to '{0}' environment.".FormatString(environmentDetails.Name)); return(environmentDetails); } if (!Environment.IsNullOrWhiteSpace()) { Trace.WriteLineIf(true, "The '{0}' environment found in the configuration file.".FormatString(Environment)); environmentDetails = SharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(Environment); } if (environmentDetails == null) { environmentDetails = SharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(); } if ((environmentDetails != null) && environmentDetails.Freeze) { ChoApplication.Trace(true, "The '{0}' environment is locked to use by designated configured machines only. No environment will be used.".FormatString(environmentDetails.Name)); environmentDetails = null; } return(environmentDetails); } //ChoEnvironmentDetails environmentDetails = null; //lock (_padLock) //{ // environmentDetails = SharedEnvironmentManager.GetEnvironmentDetails(); // if (environmentDetails != null && environmentDetails.Freeze) // { // Environment = environmentDetails.Name; // ChoApplication.Trace(true, "This host is Freezed to '{0}' environment.".FormatString(environmentDetails.Name)); // } // else // { // environmentDetails = SharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(Environment); // if (environmentDetails == null) // { // if (!Environment.IsNullOrWhiteSpace()) // Trace.WriteLineIf(true, "The '{0}' environment found in the configuration file.".FormatString(Environment)); // environmentDetails = SharedEnvironmentManager.GetEnvironmentDetailsByEnvironment(); // } // } //} ////if (environmentDetails != null) ////{ //// Environment = environmentDetails.Name; //// ChoApplication.Trace(true, "Using the '{0}' environment.".FormatString(Environment)); //// _configFilePath = environmentDetails.AppFrxFilePath; ////} ////else ////{ //// ChoApplication.Trace(true, "No Environment found for this host."); //// _configFilePath = null; ////} //return environmentDetails; }