private void FatalApplicationException(object sender, ChoFatalErrorEventArgs e) { try { ChoApplication.WriteToEventLog(e.ToString(), System.Diagnostics.EventLogEntryType.Error); } catch { //TODO: Write to system log } }
protected virtual void OnShutdown() { try { OnStop(); } catch (Exception ex) { ChoApplication.WriteToEventLog(ex.ToString()); } }
/// <summary> /// Unhandled exception handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { //TODO: able to hook into user dll method string errorMsg = "An application error occurred. Please contact the adminstrator with the following information:\n\n"; Exception ex = (Exception)e.ExceptionObject; if (ex != null) { errorMsg = ChoApplicationException.ToString(ex); ChoApplication.WriteToEventLog(errorMsg); //errorMsg += ex.Message + "\n\nStack Trace:\n" + ex.StackTrace; //ChoApplication.WriteToEventLog(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace, EventLogEntryType.Error); } else { ChoApplication.WriteToEventLog(errorMsg + "Unknown exception occured.", EventLogEntryType.Error); } Console.WriteLine(errorMsg); Environment.FailFast(errorMsg, ex); }
public ChoApplicationHost() { ChoApplication.ApplyFrxParamsOverrides += ((sender, e) => { ApplyFrxParamsOverrides(e.GlobalApplicationSettings, e.MetaDataFilePathSettings); using (ChoCommandLineArgParser parser = new ChoCommandLineArgParser()) { parser.Parse(); foreach (KeyValuePair <string, string> keyValuePair in parser) { if (keyValuePair.Key.ToUpper() == "I") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.InstallService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.InstallService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.InstallService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "U") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.UninstallService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.UninstallService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.UninstallService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "S") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.StartService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.StartService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.StartService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "T") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.StopService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.StopService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.StopService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "P") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.PauseService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.PauseService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.PauseService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "C") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.ContinueService".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.ContinueService".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.ContinueService".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } else if (keyValuePair.Key.ToUpper() == "E") { ChoApplication.ServiceInstallation = true; e.GlobalApplicationSettings.ApplicationBehaviourSettings.HideWindow = false; e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = false; e.GlobalApplicationSettings.DoAppendProcessIdToLogDir = false; e.GlobalApplicationSettings.ApplicationName = "{0}.ExecuteCommand".FormatString(e.GlobalApplicationSettings.ApplicationNameWithoutExtension); e.GlobalApplicationSettings.EventLogSourceName = "{0}.ExecuteCommand".FormatString(e.GlobalApplicationSettings.EventLogSourceName); e.GlobalApplicationSettings.LogSettings.LogFileName = "{0}.ExecuteCommand".FormatString(e.GlobalApplicationSettings.LogSettings.LogFileName); break; } } } }); ChoApplication.FatalApplicationException += FatalApplicationException; //ChoApplication.Initialize(); ChoGlobalApplicationSettings x = ChoGlobalApplicationSettings.Me; ApplyServiceInstallParamsOverrides(ChoServiceProcessInstallerSettings.Me, ChoServiceInstallerSettings.Me); ServiceProcessInstaller processInstaller = new ServiceProcessInstaller(); ChoApplication.WriteToEventLog(ChoServiceProcessInstallerSettings.Me.ToString()); processInstaller.Account = ChoServiceProcessInstallerSettings.Me.Account; processInstaller.Username = ChoServiceProcessInstallerSettings.Me.UserName; processInstaller.Password = ChoServiceProcessInstallerSettings.Me.Password; ChoApplication.WriteToEventLog(ChoServiceInstallerSettings.Me.ToString()); ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceInstaller.DisplayName = ChoServiceInstallerSettings.Me.DisplayName.IsNullOrWhiteSpace() ? ChoGlobalApplicationSettings.Me.ApplicationNameWithoutExtension : ChoServiceInstallerSettings.Me.DisplayName; ServiceName = serviceInstaller.ServiceName = ChoServiceInstallerSettings.Me.ServiceName.IsNullOrWhiteSpace() ? ChoGlobalApplicationSettings.Me.ApplicationNameWithoutExtension : ChoServiceInstallerSettings.Me.ServiceName; serviceInstaller.StartType = ChoServiceInstallerSettings.Me.ServiceStartMode; serviceInstaller.DelayedAutoStart = ChoServiceInstallerSettings.Me.DelayedAutoStart; serviceInstaller.Description = ChoServiceInstallerSettings.Me.Description; this.Installers.Add(processInstaller); this.Installers.Add(serviceInstaller); }
public ChoApplicationHost() { Instance = this; //if (ChoApplication.ApplicationMode == ChoApplicationMode.NA) //if (!ChoApplication.IsInitialized) //{ if (!ChoApplication.InternalEventsSubscriped) { ChoApplication.InternalEventsSubscriped = true; ChoApplication.ApplyGlobalApplicationSettingsOverridesInternal += ((sender, e) => { ChoServiceCommandLineArgs.OverrideFrxParams(e.Value); ApplyGlobalApplicationSettingsOverrides(e.Value); } ); ChoApplication.AfterAppFrxSettingsLoadedInternal += ((sender, e) => { if (e.Value is ChoServiceInstallerSettings) { ChoServiceInstallerSettings o = (ChoServiceInstallerSettings)e.Value; o.BeforeInstall(() => BeforeInstall()); o.AfterInstall(() => AfterInstall()); o.BeforeUninstall(() => BeforeUninstall()); o.AfterUninstall(() => AfterUninstall()); } AfterAppFrxSettingsLoaded(e.Value); } ); ChoApplication.ApplyMetaDataFilePathSettingsOverridesInternal += ((sender, e) => { ApplyMetaDataFilePathSettingsOverrides(e.Value); } ); ChoApplication.AfterNotifyIconConstructedInternal += ((sender, e) => { AfterNotifyIconConstructed(e.Value); } ); } //ChoApplication.ApplyFrxParamsOverridesInternal += ((sender, e) => //{ // ApplyFrxParamsOverrides(e.GlobalApplicationSettings, e.MetaDataFilePathSettings); // if (_initialized) return; // _initialized = true; // ChoServiceCommandLineArgs.OverrideFrxParams(e); //}); if (Environment.UserInteractive) { if (this.GetMainWindowObject() != null) { if (ChoApplication.ApplicationMode == ChoApplicationMode.NA) { if (GetMainWindowObject() is Window || GetMainWindowObject() is Form) { ChoApplication.ApplicationMode = ChoApplicationMode.Windows; if (GetMainWindowObject() is Window) { ChoApplication.WindowsAppType = ChoWindowsAppType.WPF; } else { ChoApplication.WindowsAppType = ChoWindowsAppType.WinForms; } } else { ChoApplication.ApplicationMode = ChoApplicationMode.Console; } } //if (!(this is IChoWindowApp)) // throw new ChoApplicationException("ApplicationHost must derive from IChoWindowApp for Windows application."); } //ChoApplicationMode? applicationMode = ChoFrameworkCmdLineArgs.GetApplicationMode(); //if (applicationMode != null) // ChoApplication.ApplicationMode = applicationMode.Value; if (ChoApplication.ApplicationMode == ChoApplicationMode.Windows) { //if (ChoApplication.WindowsAppType == ChoWindowsAppType.NA) // ChoApplication.ApplicationMode = ChoApplicationMode.Console; } } ChoApplication.FatalApplicationException += FatalApplicationException; //if (Environment.UserInteractive) //{ // if (!ChoServiceCommandLineArgs.HasServiceParams()) // { // if (ChoApplication.ApplicationMode == ChoApplicationMode.Windows) // { // if (!ChoAppFrxSettings.Me.DoNotShowEnvSelectionWnd) // { // ChoChooseEnvironmentFrm.Show(); // //frm1 = new ChoChooseEnvironmentFrm(); // //if (frm1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) // // Environment.Exit(-101); // } // } // } //} //} //else // ApplyFrxParamsOverrides(ChoGlobalApplicationSettings.Me, ChoMetaDataFilePathSettings.Me); //ChoApplication.Initialize(); ChoGlobalApplicationSettings x = ChoGlobalApplicationSettings.Me; ChoApplication.WriteToEventLog(ChoServiceInstallerSettings.Me.ToString()); }
private static void Initialize() { if (_isInitialized) { return; } lock (_padLock) { if (_isInitialized) { return; } _logBackupDay = DateTime.Today; InitializeAppInfo(); if (!ServiceInstallation) { if (ApplicationMode != ChoApplicationMode.Service && ApplicationMode != ChoApplicationMode.Web) { try { _rkAppRun = Registry.CurrentUser.OpenSubKey(RegRunSubKey, true); if (_rkAppRun == null) { _rkAppRun = Registry.CurrentUser.CreateSubKey(RegRunSubKey); } RunAtSystemStartup(!ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.RunAtStartup); } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); } try { _rkAppRunOnce = Registry.CurrentUser.OpenSubKey(RegRunOnceSubKey, true); if (_rkAppRunOnce == null) { _rkAppRunOnce = Registry.CurrentUser.CreateSubKey(RegRunOnceSubKey); } RunOnceAtSystemStartup(!ChoGlobalApplicationSettings.Me.ApplicationBehaviourSettings.RunOnceAtStartup); } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); } } } ChoGuard.ArgumentNotNullOrEmpty(ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath, "Application Config Path"); try { _elApplicationEventLog = new EventLog("Application", Environment.MachineName, ChoGlobalApplicationSettings.Me.ApplicationName); _elApplicationEventLog.Log = "Application"; _elApplicationEventLog.Source = ChoGlobalApplicationSettings.Me.EventLogSourceName; } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); } ApplicationConfigDirectory = Path.GetDirectoryName(ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath); //Add default text trace listerner, if not defined in the configuration file Directory.CreateDirectory(ChoApplication.ApplicationLogDirectory); try { if (_logFileName != ChoGlobalApplicationSettings.Me.LogSettings.LogFileName || _logDirectory != ChoApplication.ApplicationLogDirectory) { _logFileName = ChoGlobalApplicationSettings.Me.LogSettings.LogFileName; _logDirectory = ChoApplication.ApplicationLogDirectory; ChoTextWriterTraceListener frxTextWriterTraceListener = new Cinchoo.Core.Diagnostics.ChoTextWriterTraceListener("Cinchoo", String.Format("BASEFILENAME={0};DIRECTORYNAME={1};FILEEXT={2}", ChoGlobalApplicationSettings.Me.LogSettings.LogFileName, ChoApplication.ApplicationLogDirectory, ChoReservedFileExt.Txt)); if (_frxTextWriterTraceListener != null) { System.Diagnostics.Trace.Listeners.Remove(_frxTextWriterTraceListener); } else { ChoGlobalTimerServiceManager.Register("Logbackup", () => { if (DateTime.Today != _logBackupDay) { _logBackupDay = DateTime.Today; ChoTrace.Backup(); } }, 60000); } _frxTextWriterTraceListener = frxTextWriterTraceListener; System.Diagnostics.Trace.Listeners.Add(_frxTextWriterTraceListener); } } catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); } while (_queueTraceMsg.Count > 0) { Tuple <bool?, string> tuple = _queueTraceMsg.Dequeue(); System.Diagnostics.Trace.WriteLineIf(tuple.Item1 == null ? ChoGlobalApplicationSettings.Me.TurnOnConsoleOutput : tuple.Item1.Value, tuple.Item2); } _isInitialized = true; ChoApplication.WriteToEventLog(ChoApplication.ToString()); //ChoApplication.WriteToEventLog(ChoGlobalApplicationSettings.Me.ToString()); //Initialize other Framework Settings ChoAssembly.Initialize(); ChoConsole.Initialize(); ChoConfigurationManager.Initialize(); } }
public static void Exit(int exitCode, string errMsg) { ChoApplication.WriteToEventLog(errMsg, EventLogEntryType.Error); ChoApplication.OnFatalApplicationException(exitCode, errMsg); }