Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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;
                }
            }
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 protected override void OnSessionChange(SessionChangeDescription changeDescription)
 {
     ChoApplication.WriteToEventLog("OnSessionChange()...");
     if (_host != null)
     {
         _host.OnSessionChangeService(changeDescription);
     }
 }
Exemplo n.º 7
0
 protected override void OnStart(string[] args)
 {
     ChoApplication.WriteToEventLog("OnStart()...");
     if (_host != null)
     {
         _host.OnStartService(args);
     }
 }
Exemplo n.º 8
0
 protected override void OnContinue()
 {
     ChoApplication.WriteToEventLog("OnContinue()...");
     if (_host != null)
     {
         _host.OnContinueService();
     }
 }
Exemplo n.º 9
0
 protected override void OnCustomCommand(int command)
 {
     ChoApplication.WriteToEventLog("OnCustomCommand()...");
     if (_host != null)
     {
         _host.OnCustomCommandService(command);
     }
 }
Exemplo n.º 10
0
        private static void LoadConfigurationFile()
        {
            if (_appXmlDocument != null)
            {
                _appXmlDocument.Dispose();
                _appXmlDocument = null;
            }

            _appXmlDocument            = new ChoXmlDocument(_appConfigPath);
            _appIncludeConfigFilePaths = _appXmlDocument.IncludeFiles;

            if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
            {
                _appXmlDocument.XmlDocument.Save(_appConfigPath);
            }

            if (_configurationChangeWatcher == null)
            {
                _configurationChangeWatcher = new ChoAppConfigurationChangeFileWatcher("configurations", _appConfigPath, _appIncludeConfigFilePaths);
                _configurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_configurationChangeWatcher_ConfigurationChanged));
                ChoEnvironmentSettings.SetEnvironmentChangedEventHandlerNoCall("configurations", (sender, e) =>
                {
                    ChoAppConfigurationChangeFileWatcher configurationChangeWatcher = _configurationChangeWatcher;
                    _configurationChangeWatcher = null;
                    _configurationChangeWatcher_ConfigurationChanged(null, null);
                    configurationChangeWatcher.OnConfigurationChanged();
                    configurationChangeWatcher.Dispose();
                    configurationChangeWatcher = null;
                });
            }
            else
            {
                _configurationChangeWatcher.Reset(_appConfigPath, _appIncludeConfigFilePaths);
            }

            if (_systemConfigurationChangeWatcher == null)
            {
                try
                {
                    _systemConfigurationChangeWatcher = new ChoConfigurationChangeFileWatcher("systemConfigurations", AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    _systemConfigurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_systemConfigurationChangeWatcher_ConfigurationChanged));
                }
                catch (Exception ex)
                {
                    ChoApplication.Trace(ex.ToString());
                }
            }

            //Remove namespaces
            if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
            {
                XDocument doc = XDocument.Load(_appConfigPath, LoadOptions.PreserveWhitespace);
                doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
                doc.Save(_appConfigPath, SaveOptions.DisableFormatting);
            }
            _configuration = _appXmlDocument.XmlDocument.DocumentElement.ToObject <ChoConfiguration>();
            _configuration.Initialize();
        }
Exemplo n.º 11
0
        protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
        {
            ChoApplication.WriteToEventLog("OnPowerEvent()...");
            if (_host != null)
            {
                return(_host.OnPowerEventService(powerStatus));
            }

            return(true);
        }
Exemplo n.º 12
0
 public static void Backup()
 {
     try
     {
         Trace.Write(BACKUP);
     }
     catch (Exception ex)
     {
         ChoApplication.WriteToEventLog(ChoApplicationException.ToString(ex), EventLogEntryType.Error);
     }
 }
Exemplo n.º 13
0
 protected override void OnClick(EventArgs e)
 {
     if (ChoWindowsIdentity.IsAdministrator())
     {
         base.OnClick(e);
     }
     else
     {
         ChoApplication.RestartAsAdmin();
     }
 }
Exemplo n.º 14
0
 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);
     }
 }
Exemplo n.º 15
0
        public bool Initialize(bool beforeFieldInit, object state)
        {
            MachineName     = ChoSystemInfo.MachineName;
            AppDomainName   = ChoApplication.AppDomainName;
            ProcessId       = ChoApplication.ProcessId.ToString();
            ProcessName     = ChoApplication.ProcessFilePath;
            HostName        = ChoSystemInfo.HostName;
            ApplicationName = ChoGlobalApplicationSettings.Me.ApplicationName;
            ThreadId        = ChoApplication.GetThreadId().ToString();
            ThreadName      = ChoApplication.GetThreadName();

            Enrich(ChoStackTrace.GetStackFrame());

            return(false);
        }
Exemplo n.º 16
0
 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);
         }
     }
 }
Exemplo n.º 17
0
        internal static T GetSection <T>(string sectionName, T defaultValue = default(T))
            where T : ConfigurationSection
        {
            T instance = (T)ChoConfigurationManager.GetSection(sectionName);

            if (instance == null)
            {
                instance = defaultValue;
                if (!ChoAppFrxSettings.Me.DisableFrxConfig)
                {
                    instance.Save(sectionName);
                }
            }
            ChoApplication.RaiseAfterAppFrxSettingsLoaded(instance);
            return(instance);
        }
Exemplo n.º 18
0
        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);
            }
        }
Exemplo n.º 19
0
        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());
            }
        }
Exemplo n.º 20
0
        //public static event EventHandler<ChoEventArgs<ChoPathEventArgs>> ResolveFullPath;

        #endregion Shared Data Members (Private)

        #region Constructors

        static ChoPath()
        {
            try
            {
                HttpContext ctx = HttpContext.Current;
                if (ctx == null)
                {
                    _assemblyBaseDirectory = Path.GetDirectoryName(ChoAssembly.GetEntryAssembly().Location);
                }
                else
                {
                    _assemblyBaseDirectory = HttpContext.Current.Request.PhysicalApplicationPath;
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ChoApplication.Trace(true, ex.ToString());
            }
        }
Exemplo n.º 21
0
 public static void HandleException(this Exception ex, Exception newEx)
 {
     if (ex is ChoFatalApplicationException)
     {
         throw ex;
     }
     else if (ex is ChoCommandLineArgException ||
              ex is ChoCommandLineArgUsageException)
     {
         ChoApplication.DisplayMsg(ex.Message);
     }
     else
     {
         if (newEx == null)
         {
             ChoApplication.DisplayMsg(ex.Message, ex);
             throw ex;
         }
         else
         {
             throw newEx;
         }
     }
 }
Exemplo n.º 22
0
        internal static void Initialize(ChoApplicationHost host)
        {
            if (!ChoApplicationHost.IsApplicationHostUsed)
            {
                return;
            }

            if (!Environment.UserInteractive)
            {
                //Debugger.Break();
                ChoService service = new ChoService(host);

                service.CanHandlePowerEvent         = ChoServiceInstallerSettings.Me.CanHandlePowerEvent;
                service.CanHandleSessionChangeEvent = ChoServiceInstallerSettings.Me.CanHandleSessionChangeEvent;
                service.CanPauseAndContinue         = ChoServiceInstallerSettings.Me.CanPauseAndContinue;
                service.CanShutdown = ChoServiceInstallerSettings.Me.CanShutdown;
                service.CanStop     = ChoServiceInstallerSettings.Me.CanStop;
                service.AutoLog     = ChoServiceInstallerSettings.Me.AutoLog;
                //service.ExitCode = ChoServiceInstallerSettings.Me.ExitCode;

                //Windows Service Mode
                ServiceBase[] ServicesToRun = new ServiceBase[]
                {
                    service
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                //Parse command line arguments, install,
                //try
                //{
                ChoFrameworkCmdLineArgs frameworkCmdLineArgs = new ChoFrameworkCmdLineArgs();
                frameworkCmdLineArgs.Init();
                ChoServiceCommandLineArgs serviceCmdLineArgs = new ChoServiceCommandLineArgs();
                serviceCmdLineArgs.Init();

                ServiceController sc = new ServiceController(ChoServiceCommandLineArgs.GetServiceName(), Environment.MachineName);
                if (serviceCmdLineArgs.InstallService)
                {
                    ChoManagedInstallerClass.InstallService();

                    //Save the command line parameters
                    //if (serviceCmdLineArgs.ServiceParams != null)
                    //{
                    //    try
                    //    {
                    //        ChoRegistryKey _rkAppRun = new ChoRegistryKey(String.Format(RegRunSubKey.FormatString(ChoApplication.Host.ServiceName), true));
                    //        _rkAppRun.SetValue("ImagePath", "{0} {1}".FormatString(ChoAssembly.GetEntryAssembly().Location, ChoServiceCommandLineArgs.GetServiceParams().Replace("'", @"""")));
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        System.Diagnostics.Trace.TraceError(ex.ToString());
                    //    }
                    //}
                }
                else if (serviceCmdLineArgs.UninstallService)
                {
                    ChoManagedInstallerClass.UninstallService();
                }
                else if (serviceCmdLineArgs.StartService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }

                    if (serviceCmdLineArgs.ServiceParams == null ||
                        serviceCmdLineArgs.ServiceParams.Length == 0)
                    {
                        //ChoEnvironment.CommandLineArgs = Environment.GetCommandLineArgs().Skip(1).ToArray();
                    }
                    else
                    {
                        string commandLineArgs = null;
                        if (serviceCmdLineArgs.ServiceParams.StartsWith("\"") &&
                            serviceCmdLineArgs.ServiceParams.EndsWith("\""))
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams.Substring(1, serviceCmdLineArgs.ServiceParams.Length - 2);
                        }
                        else
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams;
                        }

                        ChoEnvironment.CommandLineArgs = commandLineArgs.SplitNTrim(' ');
                    }

                    sc.Start(ChoEnvironment.CommandLineArgs);

                    if (ChoServiceInstallerSettings.Me.Timeout > 0)
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(ChoServiceInstallerSettings.Me.Timeout));
                    }
                }
                else if (serviceCmdLineArgs.StopService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Stop();
                }
                else if (serviceCmdLineArgs.PauseService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Pause();
                }
                else if (serviceCmdLineArgs.ContinueService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Continue();
                }
                else if (serviceCmdLineArgs.ExecuteCommand != Int32.MinValue)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.ExecuteCommand(serviceCmdLineArgs.ExecuteCommand);
                }
                else
                {
                    //ChoApplicationMode? applicationMode = ChoFrameworkCmdLineArgs.GetApplicationMode();
                    //if (applicationMode != null)
                    //    ChoApplication.ApplicationMode = applicationMode.Value;

                    //ChoProfile.WriteLine(ChoApplication.ApplicationMode.ToString());
                    if (ChoApplication.ApplicationMode == ChoApplicationMode.Windows)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
                    {
                        if (ChoConsoleSettings.Me.ConsoleMode != uint.MinValue && ChoWindowsManager.ConsoleWindowHandle != IntPtr.Zero)
                        {
                            ChoKernel32.SetConsoleMode(ChoWindowsManager.ConsoleWindowHandle, (uint)ChoConsoleSettings.Me.ConsoleMode);
                        }

                        ChoApplicationHost.RegisterConsoleControlHandler();
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                        //ChoApplication.Host.OnStopService();
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Web)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                }
                //}
                //catch (ChoFatalApplicationException)
                //{
                //    throw;
                //}
                //catch (ChoCommandLineArgException argEx)
                //{
                //    ChoApplication.DisplayMsg(argEx.Message);
                //    throw;
                //}
                //catch (ChoCommandLineArgUsageException usageEx)
                //{
                //    ChoApplication.DisplayMsg(usageEx.Message);
                //    throw;
                //}
                //catch (Exception ex)
                //{
                //    ChoApplication.DisplayMsg(ex.Message, ex);
                //    throw;
                //}
            }
        }
Exemplo n.º 23
0
        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);
                }
            }
        }
Exemplo n.º 24
0
 public static void Main(string[] args)
 {
     ChoApplication.Run(args);
 }
Exemplo n.º 25
0
 internal static void Refresh()
 {
     ChoApplication.RaiseAfterConfigurationManagerInitialized();
 }
Exemplo n.º 26
0
        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);
                }
            }
        }
Exemplo n.º 27
0
        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);
                }
            }
        }