private void QueueProcessingThreadCallback(object state) { while (true) { try { IChoQueuedMsgServiceObject <T> msgQObject = _queue.Dequeue() as IChoQueuedMsgServiceObject <T>; if (msgQObject == null) { continue; } if (msgQObject.IsQuitServiceMsg) { break; } QueueMessageHandler(msgQObject); } catch (Exception ex) { ChoApplication.WriteToEventLog(ex.ToString(), EventLogEntryType.Error); //ChoProfile.DefaultContext.Append(ex); //if (!EventLog.SourceExists(ChoAssembly.GetEntryAssembly().GetName().Name)) // EventLog.CreateEventSource(ChoAssembly.GetEntryAssembly().GetName().Name, "Application"); //EventLog.WriteEntry(ChoAssembly.GetEntryAssembly().GetName().Name, ChoApplicationException.ToString(ex)); //throw; } } }
public ChoServiceInstaller() { ChoService.ServiceInstallerInitialization(this, new ChoServiceInstallerEventArgs(ChoServiceProcessInstallerSettings.Me, ChoServiceInstallerSettings.Me)); ServiceProcessInstaller processInstaller = new ServiceProcessInstaller(); ChoApplication.WriteToEventLog(ChoServiceProcessInstallerSettings.Me.ToString()); processInstaller.Account = ChoServiceProcessInstallerSettings.Me.Account; if (ChoServiceProcessInstallerSettings.Me.Account == ServiceAccount.User) { processInstaller.Username = ChoServiceProcessInstallerSettings.Me.UserName; processInstaller.Password = ChoServiceProcessInstallerSettings.Me.Password; } ChoApplication.WriteToEventLog(ChoServiceInstallerSettings.Me.ToString()); ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceInstaller.DisplayName = ChoServiceInstallerSettings.Me.DisplayName; serviceInstaller.StartType = ChoServiceInstallerSettings.Me.ServiceStartMode; serviceInstaller.ServiceName = ChoServiceInstallerSettings.Me.ServiceName; serviceInstaller.DelayedAutoStart = ChoServiceInstallerSettings.Me.DelayedAutoStart; serviceInstaller.Description = ChoServiceInstallerSettings.Me.Description; this.Installers.Add(processInstaller); this.Installers.Add(serviceInstaller); }
public static bool Write(Exception ex) { // return Write(ex, ChoStackTrace.GetStackFrame(typeof(ChoTrace))); //} //internal static bool Write(Exception ex, StackFrame callerStackFrame) //{ if (ex == null) { return(false); } if (!ChoApplicationException.IsProcessed(ex)) { //if (ChoTraceSwitch.Switch.TraceError) // ChoLogger.Log(callerStackFrame, String.Format("[{0}]: {1}{2}", ChoStackTrace.GetCallerName(), ex.Message, Environment.NewLine)); try { Trace.WriteLineIf(ChoTraceSwitch.Switch.TraceError, String.Format("[{0}]: {1}", ChoStackTrace.GetCallerName(), ChoApplicationException.ToString(ex))); return(true); } catch (Exception exception) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(exception), EventLogEntryType.Error); } finally { ChoApplicationException.SetProcessed(ex); } } return(false); }
private static void ExecuteShellExtensionMethod(string command, string[] args) { ChoEnvironment.CommandLineArgs = args; foreach (MethodInfo methodInfo in GetShellExtensionMethods()) { ChoShellExtensionContextMenuAttribute attr = methodInfo.GetCustomAttribute <ChoShellExtensionContextMenuAttribute>(); if (attr == null) { continue; } if (methodInfo.Name == command) { try { methodInfo.Invoke(null, new object[] { args }); } catch (Exception ex) { ChoTrace.WriteLine("Error while executing '{0}' shell extension command. \n {1}".FormatString(command, ChoApplicationException.ToString(ex))); ChoApplication.WriteToEventLog("Error while executing '{0}' shell extension command. \n {1}".FormatString(command, ChoApplicationException.ToString(ex))); } return; } } ChoApplication.WriteToEventLog("'{0}' shell extension command not found.".FormatString(command)); }
public override object DoObjectInitialize(object target) { if (target == null) { return(null); } base.DoObjectInitialize(target); Type objType = target.GetType(); Action <object> _objectInitializers; foreach (MethodInfo methodInfo in ChoType.GetMethods(objType, typeof(ChoSingletonInstanceInitializerAttribute))) { try { _objectInitializers = methodInfo.CreateDelegate <Action <object> >(target); _objectInitializers(target); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex)); } } return(target); }
protected override void OnContinue() { ChoApplication.WriteToEventLog("OnContinue()..."); if (_host != null) { _host.OnContinueService(); } }
protected override void OnCustomCommand(int command) { ChoApplication.WriteToEventLog("OnCustomCommand()..."); if (_host != null) { _host.OnCustomCommandService(command); } }
protected override void OnStart(string[] args) { ChoApplication.WriteToEventLog("OnStart()..."); if (_host != null) { _host.OnStartService(args); } }
protected override void OnSessionChange(SessionChangeDescription changeDescription) { ChoApplication.WriteToEventLog("OnSessionChange()..."); if (_host != null) { _host.OnSessionChangeService(changeDescription); } }
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { ChoApplication.WriteToEventLog("OnPowerEvent()..."); if (_host != null) { return(_host.OnPowerEventService(powerStatus)); } return(true); }
public static void Backup() { try { Trace.Write(BACKUP); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } }
internal static void WriteNewLineIf(bool condition, StackFrame callerStackFrame) { //ChoLogger.Log(callerStackFrame, Environment.NewLine); try { Trace.WriteLineIf(condition, Environment.NewLine); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } }
public virtual void Log(bool condition, string msg) { if (condition) { try { ChoFile.WriteLine(Path.Combine(LogDirectory, LogFileName), String.Format("{1}{0}{2}", Environment.NewLine, DateTime.Now.ToString(LogTimeStampFormat), msg.ToString())); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } } }
internal static void WriteIf(bool condition, StackFrame callerStackFrame, string msg) { if (msg == null) { return; } //if (condition) ChoLogger.Log(callerStackFrame, msg); try { Trace.WriteIf(condition, msg); } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } }
public ChoService() { try { //Dicover Service Installer Assembly entryAssembly = ChoAssembly.GetEntryAssembly(); Type runInstallerType = null; if (entryAssembly != null) { foreach (Type type in entryAssembly.GetTypes()) { RunInstallerAttribute runInstallerAttribute = type.GetCustomAttribute <RunInstallerAttribute>(); if (runInstallerAttribute == null) { continue; } if (typeof(ChoApplicationHost).IsAssignableFrom(type)) { runInstallerType = type; break; } } } if (runInstallerType != null) { _host = Activator.CreateInstance(runInstallerType) as ChoApplicationHost; } else { ChoApplication.WriteToEventLog("No type found with RunInstallerAttribute in the entry assembly."); } } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { ChoApplication.WriteToEventLog(ex.ToString()); } }
private void SyncFileWriter() { while (true) { TextTraceMsg item = null; try { object queueObject = null; if (_messageQ.Count > 0) { queueObject = _messageQ.Dequeue(); } else { _newMsgArrived.WaitOne(); continue; } item = (TextTraceMsg)queueObject; if (item.IsFlushMsg) { break; } switch (item.Operation) { case FileOperation.WriteLine: if (base.Writer == null) { lock (LogFileManager.SyncRoot) { base.Writer = LogFileManager.GetWriter(item.LogFilePath); if (base.Writer == null) { base.Writer = LogFileManager.Add(item.LogFilePath, new StreamWriter(new ChoFileStreamWithBackup(FilePath, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg))); } } } base.Writer.WriteLine(item.Msg); base.Writer.Flush(); break; case FileOperation.Write: if (base.Writer == null) { lock (LogFileManager.SyncRoot) { base.Writer = LogFileManager.GetWriter(item.LogFilePath); if (base.Writer == null) { base.Writer = LogFileManager.Add(item.LogFilePath, new StreamWriter(new ChoFileStreamWithBackup(FilePath, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg))); } } } base.Writer.Write(item.Msg); base.Writer.Flush(); break; case FileOperation.CreateNew: LogFileManager.Remove(item.LogFilePath); base.Writer = LogFileManager.Add(item.LogFilePath, new StreamWriter(new ChoFileStreamWithBackup(item.LogFilePath, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg))); break; case FileOperation.Rename: base.Writer = LogFileManager.Add(item.LogFilePath, new StreamWriter(new ChoFileStreamWithBackup(item.LogFilePath, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg))); if (File.Exists(item.PrevLogFilePath)) { using (StreamReader reader = File.OpenText(item.PrevLogFilePath)) base.Writer.WriteLine(reader.ReadToEnd()); LogFileManager.Remove(item.PrevLogFilePath); //File.Delete(Path.Combine(_directoryName, Path.ChangeExtension(item.PrevLogFilePath, _fileExt))); } break; } } catch (Exception ex) { string errMsg; if (item != null) { errMsg = String.Format("Error while writing the below message.{0}{1}{0}Exception: {2}", Environment.NewLine, item.ToString(), ex.ToString()); } else { errMsg = ex.ToString(); } ChoApplication.WriteToEventLog(errMsg, EventLogEntryType.Error); } } }
private void SyncFileWriter() { while (true) { try { object queueObject = null; if (_messageQ.Count > 0) { queueObject = _messageQ.Dequeue(); } else { _newMsgArrived.WaitOne(); continue; } TextTraceMsg item = (TextTraceMsg)queueObject; if (item.IsFlushMsg) { break; } switch (item.Operation) { case FileOperation.WriteLine: if (base.Writer == null) { base.Writer = new StreamWriter(new ChoFileStreamWithBackup(FileName, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg)); } base.Writer.WriteLine(item.Msg); base.Writer.Flush(); break; case FileOperation.Write: if (base.Writer == null) { base.Writer = new StreamWriter(new ChoFileStreamWithBackup(FileName, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg)); } base.Writer.Write(item.Msg); base.Writer.Flush(); break; case FileOperation.CreateNew: base.Writer.Flush(); base.Writer.Close(); base.Writer = new StreamWriter(new ChoFileStreamWithBackup(FileName, _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg)); break; case FileOperation.Rename: base.Writer.Flush(); base.Writer.Close(); base.Writer = new StreamWriter(new ChoFileStreamWithBackup(Path.Combine(_directoryName, Path.ChangeExtension(_baseFileName, _fileExt)), _maxFileSize, _maxFileCount, FileMode.Append, _cyclic, _autoBackup, _allowSplitMsg)); if (File.Exists(Path.Combine(_directoryName, Path.ChangeExtension(item.PrevLogFileName, _fileExt)))) { using (StreamReader reader = File.OpenText(Path.Combine(_directoryName, Path.ChangeExtension(item.PrevLogFileName, _fileExt)))) base.Writer.WriteLine(reader.ReadToEnd()); File.Delete(Path.Combine(_directoryName, Path.ChangeExtension(item.PrevLogFileName, _fileExt))); } break; } } catch (Exception ex) { ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error); } } }
internal static void Initialize() { if (!ChoApplicationHost.IsApplicationHostUsed) { return; } if (!Environment.UserInteractive) { //Windows Service Mode ServiceBase[] ServicesToRun = new ServiceBase[] { new ChoService() }; ServiceBase.Run(ServicesToRun); } else { //Parse command line arguments, install, try { ChoServiceCommandLineArgs serviceCmdLineArgs = new ChoServiceCommandLineArgs(); ServiceController sc = new ServiceController(ChoApplication.Host.ServiceName, Environment.MachineName); if (serviceCmdLineArgs.InstallService) { ManagedInstallerClass.InstallHelper(new string[] { ChoApplication.EntryAssemblyLocation }); } else if (serviceCmdLineArgs.UninstallService) { ManagedInstallerClass.InstallHelper(new string[] { "/u", ChoApplication.EntryAssemblyLocation }); } else if (serviceCmdLineArgs.StartService) { sc.Start(); } else if (serviceCmdLineArgs.StopService) { sc.Stop(); } else if (serviceCmdLineArgs.PauseService) { sc.Pause(); } else if (serviceCmdLineArgs.ContinueService) { sc.Continue(); } else if (serviceCmdLineArgs.ExecuteCommand != Int32.MinValue) { sc.ExecuteCommand(serviceCmdLineArgs.ExecuteCommand); } else { //Console mode if (ChoApplication.ApplicationMode == ChoApplicationMode.Console) { if (ChoConsoleSettings.Me.ConsoleMode != uint.MinValue && ChoWindowsManager.ConsoleWindowHandle != IntPtr.Zero) { ChoKernel32.SetConsoleMode(ChoWindowsManager.ConsoleWindowHandle, ChoConsoleSettings.Me.ConsoleMode); } ChoApplicationHost.RegisterConsoleControlHandler(); } ChoApplication.Host.OnStartService(ChoApplication.Host.Args); } } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { ChoApplication.WriteToEventLog(ex.ToString(), EventLogEntryType.Error); } } }