コード例 #1
0
        void Init()
        {
            if (Platform.IsMicrosoft)
            {
                HasAdminRights = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
            }

            IsWindowsService = Platform.IsMicrosoft && !Environment.UserInteractive;
            if (IsWindowsService)
            {
                // no log console + service run
                if (!HasAdminRights)
                {
                    throw new NotSupportedException("Service requires administration rights!");
                }

                LogSystem = new LogEventLog(EventLog, ServiceName);
            }
            else
            {
                CommandlineArguments = Arguments.FromEnvironment();

                // commandline run or linux daemon ?
                if (!CommandlineArguments.IsOptionPresent("daemon"))
                {
                    // no daemon -> log console
                    LogConsole       = LogConsole.Create();
                    LogConsole.Title = ServiceName + " v" + VersionInfo.InformalVersion;
                    if (CommandlineArguments.IsOptionPresent("debug"))
                    {
                        LogConsole.Level = LogLevel.Debug;
                    }

                    if (CommandlineArguments.IsOptionPresent("verbose"))
                    {
                        LogConsole.Level = LogLevel.Verbose;
                    }

                    if (LogConsole.Level < LogLevel.Information)
                    {
                        LogConsole.ExceptionMode = LogExceptionMode.Full;
                    }
                }

                // on unix do syslog
                LogSystem = LogConsole;
                if (Platform.Type == PlatformType.Linux)
                {
                    LogSystem = LogSyslog.Create();
                }
            }

            if (LogSystem != null)
            {
                LogSystem.ExceptionMode = LogExceptionMode.Full;
            }
            log.LogInfo("Service <cyan>{0}<default> initialized!", ServiceName);
        }
コード例 #2
0
        /// <summary>Does a commandline run.</summary>
        void CommandLineRun()
        {
            log.LogInfo("Initializing service <cyan>{0}<default> commandline instance...\nRelease: <magenta>{1}<default>, FileVersion: <magenta>{2}<default>", ServiceName, VersionInfo.AssemblyVersion, VersionInfo.FileVersion);
            var needAdminRights = false;

            if (Platform.IsMicrosoft)
            {
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                    case "stop":
                    case "install":
                    case "uninstall": needAdminRights = true; break;
                    }
                }

                if (needAdminRights && !HasAdminRights)
                {
                    if (Debugger.IsAttached)
                    {
                        throw new InvalidOperationException("Please debug this program in administration mode!");
                    }

                    log.LogNotice("Restarting service with administration rights!");
                    Logger.Close();
                    var processStartInfo = new ProcessStartInfo(CommandlineArguments.Command, CommandlineArguments.ToString(false) + " --wait")
                    {
                        UseShellExecute = true,
                        Verb            = "runas",
                    };
                    Process.Start(processStartInfo);
                    return;
                }
                if (HasAdminRights)
                {
                    log.LogInfo("Current user has <green>admin<default> rights.");
                }
                else
                {
                    log.LogInfo("Running in <red>debug<default> mode <red>without admin<default> rights.");
                }
            }

            bool runCommandLine;
            var  wait = CommandlineArguments.IsOptionPresent("wait");

            if (Debugger.IsAttached)
            {
                runCommandLine = true;
                log.LogInfo("<red>Debugger<default> attached.");
            }
            else
            {
                if (CommandlineArguments.Options.Count == 0)
                {
                    Help();
                    return;
                }
                runCommandLine = CommandlineArguments.IsOptionPresent("run");
            }
            bool isInteractive;

            if (Platform.IsMicrosoft)
            {
                ServiceHelper.ServiceName = ServiceName;
                isInteractive             = Environment.UserInteractive;
                if (CommandlineArguments.IsHelpOptionFound())
                {
                    Help();
                    return;
                }
                foreach (var option in CommandlineArguments.Options)
                {
                    switch (option.Name)
                    {
                    case "start":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StartService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>start<default> service command, doing commandline run!");
                        }

                        break;

                    case "stop":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.StopService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>stop<default> service command, doing commandline run!");
                        }

                        break;

                    case "install":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.InstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>install<default> service command, doing commandline run!");
                        }

                        break;

                    case "uninstall":
                        if (Debugger.IsAttached || !runCommandLine)
                        {
                            ServiceHelper.UnInstallService();
                        }
                        else
                        {
                            log.LogError("Ignore <red>uninstall<default> service command, doing commandline run!");
                        }

                        break;
                    }
                }
            }
            else
            {
                isInteractive  = !CommandlineArguments.IsOptionPresent("daemon");
                runCommandLine = true;
            }
            if (runCommandLine)
            {
                // --- start service as program
                ServiceParameters = new ServiceParameters(HasAdminRights, true, isInteractive);
                try
                {
                    RunWorker();
                }
                catch (Exception ex)
                {
                    log.LogError("Error while running service executable in commandline mode.\n" + ex.ToXT());
                }

                // --- exit
            }
            Logger.Flush();
            if (isInteractive && wait)
            {
                SystemConsole.RemoveKeyPressedEvent();
                while (SystemConsole.KeyAvailable)
                {
                    SystemConsole.ReadKey();
                }

                SystemConsole.WriteLine("--- Press <yellow>enter<default> to exit... ---");
                while (SystemConsole.ReadKey().Key != ConsoleKey.Enter)
                {
                }
            }
        }