main/OpenCover.Console/Program.cs
/// <summary>
/// This is the initial console harness - it may become the full thing
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
This is the initial console harness - it may become the full thing
This is the initial console harness - it may become the full thing
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);
                    var registered = false;
                    try
                    {
                        if (parser.Register)
                        {
                            ProfilerRegistration.Register(parser.Registration);
                            registered = true;
                        }
                        var harness = container.Resolve<IProfilerManager>();
                        harness.RunProcess((environment) =>
                                               {
                                                   returnCode = 0;
                                                   if (parser.Service)
                                                   {
                                                       RunService(parser, environment, logger);
                                                   }
                                                   else
                                                   {
                                                       returnCode = RunProcess(parser, environment);
                                                   }
                                               }, parser.Service);
                        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;
        }
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 ( " " ) ; } } using ( var container = new Bootstrapper ( logger ) ) { var persistance = new FilePersistance ( parser , logger ) ; container . Initialise ( filter , parser , persistance , perfCounter ) ; persistance . Initialise ( outputFile ) ; var registered = false ; try { if ( parser . Register ) { ProfilerRegistration . Register ( parser . Registration ) ; registered = true ; } var harness = container . Resolve < IProfilerManager > ( ) ; harness . RunProcess ( ( environment ) => { returnCode = 0 ; if ( parser . Service ) { RunService ( parser , environment , logger ) ; } else { returnCode = RunProcess ( parser , environment ) ; } } , parser . Service ) ; DisplayResults ( persistance , parser , logger ) ; } catch ( Exception ex ) { Trace . WriteLine ( string . Format ( " \n " , ex . Message , ex . InnerException ) ) ; throw ; } finally { if ( parser . Register && registered ) ProfilerRegistration . Unregister ( parser . Registration ) ; } } perfCounter . ResetCounters ( ) ; } catch ( Exception ex ) { if ( logger . IsFatalEnabled ) { logger . FatalFormat ( " " , ex . Message ) ; logger . FatalFormat ( " " , ex . StackTrace ) ; } returnCode = returnCodeOffset + 1 ; } return returnCode ; }
This is the initial console harness - it may become the full thing
This is the initial console harness - it may become the full thing
None
args
string[]
None