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; } }
public static void Exit(int exitCode, string errMsg) { ChoApplication.WriteToEventLog(errMsg, EventLogEntryType.Error); ChoApplication.OnFatalApplicationException(exitCode, errMsg); }