Пример #1
1
        private static void RunService(CommandLineParser parser, Action<StringDictionary> environment, ILog logger)
        {
            var service = new ServiceController(parser.Target);
            if (service.Status != ServiceControllerStatus.Stopped)
            {
                logger.ErrorFormat("The service '{0}' is already running. The profiler cannot attach to an already running service.",
                    parser.Target);
                return;
            }

            // now to set the environment variables
            var profilerEnvironment = new StringDictionary();
            environment(profilerEnvironment);

            var serviceEnvironment = new ServiceEnvironmentManagement();

            try
            {
                serviceEnvironment.PrepareServiceEnvironment(parser.Target,
                    (from string key in profilerEnvironment.Keys select string.Format("{0}={1}", key, profilerEnvironment[key])).ToArray());

                // now start the service
                service = new ServiceController(parser.Target);
                service.Start();
                logger.InfoFormat("Service starting '{0}'", parser.Target);
                service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                logger.InfoFormat("Service started '{0}'", parser.Target);
            }
            finally
            {
                // once the serice has started set the environment variables back - just in case
                serviceEnvironment.ResetServiceEnvironment();
            }

            // and wait for it to stop
            service.WaitForStatus(ServiceControllerStatus.Stopped);
            logger.InfoFormat("Service stopped '{0}'", parser.Target);
        }
Пример #2
0
        private static void RunService(CommandLineParser parser, Action <StringDictionary> environment, ILog logger)
        {
            if (ServiceEnvironmentManagementEx.IsServiceDisabled(parser.Target))
            {
                logger.ErrorFormat("The service '{0}' is disabled. Please enable the service.",
                                   parser.Target);
                return;
            }

            var service = new ServiceController(parser.Target);

            try
            {
                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    logger.ErrorFormat(
                        "The service '{0}' is already running. The profiler cannot attach to an already running service.",
                        parser.Target);
                    return;
                }

                // now to set the environment variables
                var profilerEnvironment = new StringDictionary();
                environment(profilerEnvironment);

                var serviceEnvironment = new ServiceEnvironmentManagement();

                try
                {
                    serviceEnvironment.PrepareServiceEnvironment(
                        parser.Target,
                        parser.ServiceEnvironment,
                        (from string key in profilerEnvironment.Keys
                         select string.Format("{0}={1}", key, profilerEnvironment[key])).ToArray());

                    // now start the service
                    var old = service;
                    service = new ServiceController(parser.Target);
                    old.Dispose();

                    if (parser.Target.ToLower().Equals("w3svc"))
                    {
                        // Service will not automatically start
                        if (!TerminateCurrentW3SvcHost() ||
                            !ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                        {
                            service.Start();
                        }
                    }
                    else
                    {
                        service.Start();
                    }
                    logger.InfoFormat("Service starting '{0}'", parser.Target);
                    service.WaitForStatus(ServiceControllerStatus.Running, parser.ServiceStartTimeout);
                    logger.InfoFormat("Service started '{0}'", parser.Target);
                }
                catch (InvalidOperationException fault)
                {
                    logger.FatalFormat("Service launch failed with '{0}'", fault);
                }
                finally
                {
                    // once the serice has started set the environment variables back - just in case
                    serviceEnvironment.ResetServiceEnvironment();
                }

                // and wait for it to stop
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                logger.InfoFormat("Service stopped '{0}'", parser.Target);

                // Stopping w3svc host
                if (parser.Target.ToLower().Equals("w3svc"))
                {
                    logger.InfoFormat("Stopping svchost to clean up environment variables for {0}", parser.Target);
                    if (ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                    {
                        logger.InfoFormat("Please note that the 'w3svc' service may automatically start");
                    }
                    TerminateCurrentW3SvcHost();
                }
            }
            finally
            {
                service.Dispose();
            }
        }
Пример #3
0
        /// <summary>
        /// This is the initial console harness - it may become the full thing
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        static int Main(string[] args)
        {
            var returnCode       = 0;
            var returnCodeOffset = 0;
            var logger           = LogManager.GetLogger(typeof(Bootstrapper));

            try
            {
                CommandLineParser parser;
                if (!ParseCommandLine(args, out parser))
                {
                    return(parser.ReturnCodeOffset + 1);
                }

                LogManager.GetRepository().Threshold = parser.LogLevel;

                returnCodeOffset = parser.ReturnCodeOffset;
                var filter = BuildFilter(parser);

                string outputFile;
                if (!GetFullOutputFile(parser, out outputFile))
                {
                    return(returnCodeOffset + 1);
                }

                IPerfCounters perfCounter = new NullPerfCounter();
                if (parser.EnablePerformanceCounters)
                {
                    if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
                    {
                        perfCounter = new PerfCounters();
                    }
                    else
                    {
                        throw  new InvalidCredentialException("You must be running as an Administrator to enable performance counters.");
                    }
                }

                using (var container = new Bootstrapper(logger))
                {
                    var persistance = new FilePersistance(parser, logger);
                    container.Initialise(filter, parser, persistance, perfCounter);
                    persistance.Initialise(outputFile, parser.MergeExistingOutputFile);
                    var registered = false;

                    try
                    {
                        if (parser.Register)
                        {
                            ProfilerRegistration.Register(parser.Registration);
                            registered = true;
                        }

                        var harness = container.Resolve <IProfilerManager>();

                        var servicePrincipal =
                            (parser.Service
                                ? new[] { ServiceEnvironmentManagement.MachineQualifiedServiceAccountName(parser.Target) }
                                : new string[0]).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();

                        harness.RunProcess(environment =>
                        {
                            returnCode = 0;
                            if (parser.Service)
                            {
                                RunService(parser, environment, logger);
                            }
                            else
                            {
                                returnCode = RunProcess(parser, environment);
                            }
                        }, servicePrincipal);

                        DisplayResults(persistance, parser, logger);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(string.Format("Exception: {0}\n{1}", ex.Message, ex.InnerException));
                        throw;
                    }
                    finally
                    {
                        if (parser.Register && registered)
                        {
                            ProfilerRegistration.Unregister(parser.Registration);
                        }
                    }
                }

                perfCounter.ResetCounters();
            }
            catch (Exception ex)
            {
                if (logger.IsFatalEnabled)
                {
                    logger.FatalFormat("An exception occured: {0}", ex.Message);
                    logger.FatalFormat("stack: {0}", ex.StackTrace);
                }

                returnCode = returnCodeOffset + 1;
            }

            return(returnCode);
        }
Пример #4
0
        private static void RunService(CommandLineParser parser, Action<StringDictionary> environment, ILog logger)
        {
            if (ServiceEnvironmentManagementEx.IsServiceDisabled(parser.Target))
            {
                logger.ErrorFormat("The service '{0}' is disabled. Please enable the service.",
                    parser.Target);
                return;
            }

            var service = new ServiceController(parser.Target);
            try
            {

                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    logger.ErrorFormat(
                        "The service '{0}' is already running. The profiler cannot attach to an already running service.",
                    parser.Target);
                    return;
                }

                // now to set the environment variables
                var profilerEnvironment = new StringDictionary();
                environment(profilerEnvironment);

                var serviceEnvironment = new ServiceEnvironmentManagement();

                try
                {
                    serviceEnvironment.PrepareServiceEnvironment(
                        parser.Target,
                            parser.ServiceEnvironment,
                        (from string key in profilerEnvironment.Keys
                         select string.Format("{0}={1}", key, profilerEnvironment[key])).ToArray());

                    // now start the service
                    var old = service;
                    service = new ServiceController(parser.Target);
                    old.Dispose();

                    if (parser.Target.ToLower().Equals("w3svc"))
                    {
                        // Service will not automatically start
                        if (! TerminateCurrentW3SvcHost(logger) ||
                            !ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                        {
                            service.Start();
                        }
                    }
                    else
                    {
                        service.Start();
                    }
                    logger.InfoFormat("Service starting '{0}'", parser.Target);
                    service.WaitForStatus(ServiceControllerStatus.Running, parser.ServiceStartTimeout);
                    logger.InfoFormat("Service started '{0}'", parser.Target);
                }
                catch (InvalidOperationException fault)
                {
                    logger.FatalFormat("Service launch failed with '{0}'", fault);
                }
                finally
                {
                    // once the serice has started set the environment variables back - just in case
                    serviceEnvironment.ResetServiceEnvironment();
                }

                // and wait for it to stop
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                logger.InfoFormat("Service stopped '{0}'", parser.Target);

                // Stopping w3svc host
                if (parser.Target.ToLower().Equals("w3svc"))
                {
                    logger.InfoFormat("Stopping svchost to clean up environment variables for w3svc", parser.Target);
                    if (ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                    {
                        logger.InfoFormat("Please note that the 'w3svc' service may automatically start");
                    }
                    TerminateCurrentW3SvcHost(logger);
                }
            }
            finally
            {
                service.Dispose();
            }
        }