Skip to content

Commit 169e227

Browse files
committed
WIP: More logging and notes
1 parent 70a5278 commit 169e227

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private ConsoleReplKind GetReplKind()
402402

403403
if (Stdio || !EnableConsoleRepl)
404404
{
405-
_logger.Log(PsesLogLevel.Diagnostic, "REPL configured as None");
405+
_logger.Log(PsesLogLevel.Verbose, "REPL configured as None");
406406
return ConsoleReplKind.None;
407407
}
408408

@@ -413,11 +413,11 @@ private ConsoleReplKind GetReplKind()
413413

414414
if (UseLegacyReadLine || (!s_isWindows && majorVersion == 6))
415415
{
416-
_logger.Log(PsesLogLevel.Diagnostic, "REPL configured as Legacy");
416+
_logger.Log(PsesLogLevel.Verbose, "REPL configured as Legacy");
417417
return ConsoleReplKind.LegacyReadLine;
418418
}
419419

420-
_logger.Log(PsesLogLevel.Diagnostic, "REPL configured as PSReadLine");
420+
_logger.Log(PsesLogLevel.Verbose, "REPL configured as PSReadLine");
421421
return ConsoleReplKind.PSReadLine;
422422
}
423423

src/PowerShellEditorServices.Hosting/Configuration/TransportConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ private DuplexNamedPipeTransportConfig(HostLogger logger, string pipeName)
108108

109109
public async Task<(Stream inStream, Stream outStream)> ConnectStreamsAsync()
110110
{
111-
_logger.Log(PsesLogLevel.Diagnostic, "Creating named pipe");
111+
_logger.Log(PsesLogLevel.Verbose, "Creating named pipe");
112112
NamedPipeServerStream namedPipe = NamedPipeUtils.CreateNamedPipe(_pipeName, PipeDirection.InOut);
113-
_logger.Log(PsesLogLevel.Diagnostic, "Waiting for named pipe connection");
113+
_logger.Log(PsesLogLevel.Verbose, "Waiting for named pipe connection");
114114
await namedPipe.WaitForConnectionAsync().ConfigureAwait(false);
115-
_logger.Log(PsesLogLevel.Diagnostic, "Named pipe connected");
115+
_logger.Log(PsesLogLevel.Verbose, "Named pipe connected");
116116
return (namedPipe, namedPipe);
117117
}
118118
}

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ private void UpdatePSModulePath()
263263

264264
private void LogHostInformation()
265265
{
266-
_logger.Log(PsesLogLevel.Diagnostic, "Logging host information");
267-
268-
_logger.Log(PsesLogLevel.Diagnostic, $@"
269-
PID: {System.Diagnostics.Process.GetCurrentProcess().Id}
270-
");
266+
_logger.Log(PsesLogLevel.Verbose, $"PID: {System.Diagnostics.Process.GetCurrentProcess().Id}");
271267

272268
_logger.Log(PsesLogLevel.Verbose, $@"
273269
== Build Details ==

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using Microsoft.PowerShell.EditorServices.Server;
54
using System;
65
using System.Collections.Generic;
76
using System.IO;
87
using System.Threading.Tasks;
8+
using Microsoft.PowerShell.EditorServices.Server;
99

1010
namespace Microsoft.PowerShell.EditorServices.Hosting
1111
{
1212
/// <summary>
13-
/// Class to manage the startup of PowerShell Editor Services.
14-
/// This should be called by <see cref="EditorServicesLoader"/> only after Editor Services has been loaded.
13+
/// Class to manage the startup of PowerShell Editor Services. This should be called by <see
14+
/// cref="EditorServicesLoader"/> only after Editor Services has been loaded. It relies on <see
15+
/// cref="EditorServicesServerFactory"/> to indirectly load <see
16+
/// cref="Microsoft.Extensions.Logging"/> and <see
17+
/// cref="Microsoft.Extensions.DependencyInjection"/>.
1518
/// </summary>
1619
internal class EditorServicesRunner : IDisposable
1720
{
@@ -36,18 +39,21 @@ public EditorServicesRunner(
3639
_logger = logger;
3740
_config = config;
3841
_sessionFileWriter = sessionFileWriter;
42+
// NOTE: This factory helps to isolate `Microsoft.Extensions.Logging/DependencyInjection`.
3943
_serverFactory = EditorServicesServerFactory.Create(_config.LogPath, (int)_config.LogLevel, logger);
4044
_alreadySubscribedDebug = false;
4145
_loggersToUnsubscribe = loggersToUnsubscribe;
4246
}
4347

4448
/// <summary>
4549
/// Start and run Editor Services and then wait for shutdown.
50+
///
51+
/// TODO: Use "Async" suffix in names of methods that return an awaitable type.
4652
/// </summary>
4753
/// <returns>A task that ends when Editor Services shuts down.</returns>
4854
public Task RunUntilShutdown()
4955
{
50-
// Start Editor Services
56+
// Start Editor Services (see function below)
5157
Task runAndAwaitShutdown = CreateEditorServicesAndRunUntilShutdown();
5258

5359
// Now write the session file
@@ -59,13 +65,23 @@ public Task RunUntilShutdown()
5965
return runAndAwaitShutdown;
6066
}
6167

68+
/// <summary>
69+
/// TODO: This class probably should not be <see cref="IDisposable"/> as the primary
70+
/// intention of that interface is to provide cleanup of unmanaged resources, which the
71+
/// logger certainly is not. Nor is this class used with a <see langword="using"/>. It is
72+
/// only because of the use of <see cref="_serverFactory"/> that this class is also
73+
/// disposable, and instead that class should be fixed.
74+
/// </summary>
6275
public void Dispose()
6376
{
6477
_serverFactory.Dispose();
6578
}
6679

6780
/// <summary>
68-
/// Master method for instantiating, running and waiting for the LSP and debug servers at the heart of Editor Services.
81+
/// Master method for instantiating, running and waiting for the LSP and debug servers at
82+
/// the heart of Editor Services. Uses <see cref="HostStartupInfo"/>.
83+
///
84+
/// NOTE: This is essentially <c>main</c>.
6985
/// </summary>
7086
/// <returns>A task that ends when Editor Services shuts down.</returns>
7187
private async Task CreateEditorServicesAndRunUntilShutdown()

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ public void Initialize(
351351
this.CurrentRunspace = this.initialRunspace;
352352

353353
// Write out the PowerShell version for tracking purposes
354-
this.logger.LogInformation(
355-
string.Format(
356-
"PowerShell runtime version: {0}, edition: {1}",
357-
this.LocalPowerShellVersion.Version,
358-
this.LocalPowerShellVersion.Edition));
354+
this.logger.LogInformation($"PowerShell Version: {this.LocalPowerShellVersion.Version}, Edition: {this.LocalPowerShellVersion.Edition}");
359355

360356
Version powerShellVersion = this.LocalPowerShellVersion.Version;
361357
if (powerShellVersion >= new Version(5, 0))
@@ -463,6 +459,7 @@ private static bool CheckIfRunspaceNeedsEventHandlers(RunspaceDetails runspaceDe
463459

464460
private void ConfigureRunspace(RunspaceDetails runspaceDetails)
465461
{
462+
this.logger.LogDebug("Configuring Runspace");
466463
runspaceDetails.Runspace.StateChanged += this.HandleRunspaceStateChanged;
467464
if (runspaceDetails.Runspace.Debugger != null)
468465
{
@@ -475,6 +472,7 @@ private void ConfigureRunspace(RunspaceDetails runspaceDetails)
475472

476473
private void CleanupRunspace(RunspaceDetails runspaceDetails)
477474
{
475+
this.logger.LogDebug("Cleaning Up Runspace");
478476
runspaceDetails.Runspace.StateChanged -= this.HandleRunspaceStateChanged;
479477
if (runspaceDetails.Runspace.Debugger != null)
480478
{
@@ -1933,6 +1931,9 @@ private void PowerShellContext_ExecutionStatusChangedAsync(object sender, Execut
19331931
var options = e?.ExecutionOptions;
19341932
if (options == null || !options.IsReadLine)
19351933
{
1934+
// TODO: This should somehow check if the server has actually started because we are
1935+
// currently sending this notification before it has initialized, which is not
1936+
// allowed. We should also figure out exactly _why_ that's happening.
19361937
_languageServer?.SendNotification(
19371938
"powerShell/executionStatusChanged",
19381939
e);
@@ -2144,6 +2145,7 @@ private static string GetStringForPSCommand(PSCommand psCommand)
21442145

21452146
private void SetExecutionPolicy()
21462147
{
2148+
this.logger.LogTrace("Setting Execution Policy");
21472149
// We want to get the list hierarchy of execution policies
21482150
// Calling the cmdlet is the simplest way to do that
21492151
IReadOnlyList<PSObject> policies = this.powerShell
@@ -2571,7 +2573,7 @@ private void ConfigureRunspaceCapabilities(RunspaceDetails runspaceDetails)
25712573

25722574
private void PushRunspace(RunspaceDetails newRunspaceDetails)
25732575
{
2574-
this.logger.LogTrace(
2576+
this.logger.LogDebug(
25752577
$"Pushing {this.CurrentRunspace.Location} ({this.CurrentRunspace.Context}), new runspace is {newRunspaceDetails.Location} ({newRunspaceDetails.Context}), connection: {newRunspaceDetails.ConnectionString}");
25762578

25772579
RunspaceDetails previousRunspace = this.CurrentRunspace;

0 commit comments

Comments
 (0)