/*
         * Test mode allows testing of ServiceFabricAutopilotAgent outside an Autopilot hosting environment.
         * In test mode, all dependencies on AP Runtime are replaced by test environment equivalences:
         *      Logging => text log
         *      Configuration => default test configurations
         */
        public void Start(bool useTestMode = false)
        {
            string agentApplicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            if (!useTestMode)
            {
                APRuntime.Initialize(StringConstants.ApplicationConfigurationFileName);
            }

            TextLogger.InitializeLogging(StringConstants.CustomLogIdName, useTestMode, StringConstants.TestModeLogFileNameTemplate, StringConstants.TestModeLogDirectoryName, StringConstants.TestModeTraceListenerName);

            TextLogger.LogInfo("Starting Service Fabric Autopilot Agent.");

            if (!ConfigurationManager.GetCurrentConfigurations(useTestMode))
            {
                this.Exit(false);

                return;
            }

            TextLogger.SetLogLevel(ConfigurationManager.LogLevel);

            ConfigurationManager.LogCurrentConfigurations();

            ServiceFabricLocalNodeManager localNodeManager;

            if (!ServiceFabricLocalNodeManager.Create(agentApplicationDirectory, useTestMode, out localNodeManager))
            {
                this.Exit(false);

                return;
            }

            this.localNodeManager = localNodeManager;

            this.agentStatus.InitializeAgentStatus();

            this.RegisterConsoleCancelEventNotifications();

            this.backgroundStateMachineTask = Task.Factory.StartNew(
                () => this.RunBackgroundStateMachine(this.backgroundStateMachineCancellationTokenSource.Token),
                this.backgroundStateMachineCancellationTokenSource.Token,
                TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
        }
        public void Start()
        {
            string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            this.certificateManager = new EnvironmentCertificateManager(applicationDirectory);

            APRuntime.Initialize(StringConstants.ApplicationConfigurationFileName);

            TextLogger.InitializeLogging(StringConstants.CustomLogIdName);

            TextLogger.LogInfo("Starting Service Fabric Environment Sync.");

            if (!ConfigurationManager.GetCurrentConfigurations())
            {
                this.Exit(false);

                return;
            }

            TextLogger.SetLogLevel(ConfigurationManager.LogLevel);

            if (!this.certificateManager.GetEnvironmentCertificatesFromCurrentConfigurations())
            {
                this.Exit(false);

                return;
            }

            this.RegisterConsoleCancelEventNotifications();

            this.certificateManagerTask = Task.Factory.StartNew(
                () => this.RunCertificateManager(this.certificateManagerCancellationTokenSource.Token),
                this.certificateManagerCancellationTokenSource.Token,
                TaskCreationOptions.LongRunning,
                TaskScheduler.Default);
        }