Beispiel #1
0
        private static int RunWithContainer(CommandLineParser parser, Bootstrapper container, IPersistance persistance)
        {
            var returnCode = 0;
            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 =>
                {
                    if (parser.Service)
                    {
                        RunService(parser, environment, Logger);
                        returnCode = 0;
                    }
                    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);
                }
            }
            return(returnCode);
        }
Beispiel #2
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);
        }