Exemple #1
0
        private void StartLogging()
        {
            _logger = new HostLogger(LogLevel);

            // We need to not write log messages to Stdio
            // if it's being used as a protocol transport
            if (!Stdio)
            {
                var hostLogger = new PSHostLogger(Host.UI);
                _loggerUnsubscribers.Add(_logger.Subscribe(hostLogger));
            }

            string logDirPath = GetLogDirPath();
            string logPath    = Path.Combine(logDirPath, "StartEditorServices.log");

            // Temp debugging sessions may try to reuse this same path,
            // so we ensure they have a unique path
            if (File.Exists(logPath))
            {
                int randomInt = new Random().Next();
                logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}.log");
            }

            var fileLogger = StreamLogger.CreateWithNewFile(logPath);

            _disposableResources.Add(fileLogger);
            IDisposable fileLoggerUnsubscriber = _logger.Subscribe(fileLogger);

            fileLogger.AddUnsubscriber(fileLoggerUnsubscriber);
            _loggerUnsubscribers.Add(fileLoggerUnsubscriber);

            _logger.Log(PsesLogLevel.Diagnostic, "Logging started");
        }
        private void StartLogging()
        {
            _logger = new HostLogger(LogLevel);

            // We need to not write log messages to Stdio
            // if it's being used as a protocol transport
            if (!Stdio)
            {
                var hostLogger = new PSHostLogger(Host.UI);
                _loggerUnsubscribers.Add(_logger.Subscribe(hostLogger));
            }

            string logPath    = Path.Combine(GetLogDirPath(), "StartEditorServices.log");
            var    fileLogger = StreamLogger.CreateWithNewFile(logPath);

            _disposableResources.Add(fileLogger);
            IDisposable fileLoggerUnsubscriber = _logger.Subscribe(fileLogger);

            fileLogger.AddUnsubscriber(fileLoggerUnsubscriber);
            _loggerUnsubscribers.Add(fileLoggerUnsubscriber);

            _logger.Log(PsesLogLevel.Diagnostic, "Logging started");
        }
        /// <summary>
        /// Master method for instantiating, running and waiting for the LSP and debug servers at the heart of Editor Services.
        /// </summary>
        /// <returns>A task that ends when Editor Services shuts down.</returns>
        private async Task CreateEditorServicesAndRunUntilShutdown()
        {
            try
            {
                bool creatingLanguageServer = _config.LanguageServiceTransport != null;
                bool creatingDebugServer    = _config.DebugServiceTransport != null;
                bool isTempDebugSession     = creatingDebugServer && !creatingLanguageServer;

                // Set up information required to instantiate servers
                HostStartupInfo hostStartupInfo = CreateHostStartupInfo();

                // If we just want a temp debug session, run that and do nothing else
                if (isTempDebugSession)
                {
                    await RunTempDebugSessionAsync(hostStartupInfo).ConfigureAwait(false);

                    return;
                }

                // We want LSP and maybe debugging
                // To do that we:
                //  - Create the LSP server
                //  - Possibly kick off the debug server creation
                //  - Start the LSP server
                //  - Possibly start the debug server
                //  - Wait for the LSP server to finish

                // Unsubscribe the host logger here so that the integrated console is not polluted with input after the first prompt
                _logger.Log(PsesLogLevel.Verbose, "Starting server, deregistering host logger and registering shutdown listener");
                if (_loggersToUnsubscribe != null)
                {
                    foreach (IDisposable loggerToUnsubscribe in _loggersToUnsubscribe)
                    {
                        loggerToUnsubscribe.Dispose();
                    }
                }

                WriteStartupBanner();

                PsesLanguageServer languageServer = await CreateLanguageServerAsync(hostStartupInfo).ConfigureAwait(false);

                Task <PsesDebugServer> debugServerCreation = null;
                if (creatingDebugServer)
                {
                    debugServerCreation = CreateDebugServerWithLanguageServerAsync(languageServer, usePSReadLine: _config.ConsoleRepl == ConsoleReplKind.PSReadLine);
                }

                languageServer.StartAsync();

                if (creatingDebugServer)
                {
                    StartDebugServer(debugServerCreation);
                }

                await languageServer.WaitForShutdown().ConfigureAwait(false);
            }
            finally
            {
                // Resubscribe host logger to log shutdown events to the console
                _logger.Subscribe(new PSHostLogger(_config.PSHost.UI));
            }
        }