Skip to content

Commit 8522e0f

Browse files
committed
Clean up logging in PowerShellContextService
I added a bunch more recently that was became less useful and more noisy after my familiarity improved.
1 parent ee1a2ed commit 8522e0f

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private void WriteStartupBanner()
308308

309309
private void DebugServer_OnSessionEnded(object sender, EventArgs args)
310310
{
311-
_logger.Log(PsesLogLevel.Verbose, "Debug session ended. Restarting debug service");
311+
_logger.Log(PsesLogLevel.Verbose, "Debug session ended, restarting debug service...");
312312
var oldServer = (PsesDebugServer)sender;
313313
oldServer.Dispose();
314314
_alreadySubscribedDebug = false;

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+12-35
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ public PowerShellContextService(
192192
OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServerFacade languageServer,
193193
bool isPSReadLineEnabled)
194194
{
195-
logger.LogTrace("Instantiating PowerShellContextService and adding event handlers");
196195
_languageServer = languageServer;
197196
this.logger = logger;
198197
this.isPSReadLineEnabled = isPSReadLineEnabled;
@@ -214,7 +213,7 @@ public static PowerShellContextService Create(
214213
// Respect a user provided bundled module path.
215214
if (Directory.Exists(hostStartupInfo.BundledModulePath))
216215
{
217-
logger.LogTrace($"Using new bundled module path: {hostStartupInfo.BundledModulePath}");
216+
logger.LogDebug($"Using new bundled module path: {hostStartupInfo.BundledModulePath}");
218217
s_bundledModulePath = hostStartupInfo.BundledModulePath;
219218
}
220219

@@ -238,7 +237,6 @@ public static PowerShellContextService Create(
238237
hostUserInterface,
239238
logger);
240239

241-
logger.LogTrace("Creating initial PowerShell runspace");
242240
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
243241
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
244242
powerShellContext.ImportCommandsModuleAsync();
@@ -327,7 +325,6 @@ public void Initialize(
327325
IHostOutput consoleHost)
328326
{
329327
Validate.IsNotNull("initialRunspace", initialRunspace);
330-
this.logger.LogTrace($"Initializing PowerShell context with runspace {initialRunspace.Name}");
331328

332329
this.ownsInitialRunspace = ownsInitialRunspace;
333330
this.SessionState = PowerShellContextState.NotStarted;
@@ -363,6 +360,7 @@ public void Initialize(
363360
}
364361
else
365362
{
363+
// TODO: Also throw for PowerShell 6
366364
throw new NotSupportedException(
367365
"This computer has an unsupported version of PowerShell installed: " +
368366
powerShellVersion.ToString());
@@ -577,10 +575,9 @@ public Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
577575
cancellationToken);
578576
}
579577

580-
581578
/// <summary>
582579
/// Executes a PSCommand against the session's runspace and returns
583-
/// a collection of results of the expected type.
580+
/// a collection of results of the expected type. This function needs help.
584581
/// </summary>
585582
/// <typeparam name="TResult">The expected result type.</typeparam>
586583
/// <param name="psCommand">The PSCommand to be executed.</param>
@@ -601,8 +598,6 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
601598
Validate.IsNotNull(nameof(psCommand), psCommand);
602599
Validate.IsNotNull(nameof(executionOptions), executionOptions);
603600

604-
this.logger.LogTrace($"Attempting to execute command(s): {GetStringForPSCommand(psCommand)}");
605-
606601
// Add history to PSReadLine before cancelling, otherwise it will be restored as the
607602
// cancelled prompt when it's called again.
608603
if (executionOptions.AddToHistory)
@@ -636,8 +631,6 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
636631
this.ShouldExecuteWithEventing(executionOptions) ||
637632
(PromptNest.IsRemote && executionOptions.IsReadLine)))
638633
{
639-
this.logger.LogTrace("Passing command execution to pipeline thread");
640-
641634
if (shouldCancelReadLine && PromptNest.IsReadLineBusy())
642635
{
643636
// If a ReadLine pipeline is running in the debugger then we'll stop responding here
@@ -715,6 +708,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
715708
}
716709
try
717710
{
711+
this.logger.LogTrace($"Executing in debugger: {GetStringForPSCommand(psCommand)}");
718712
return this.ExecuteCommandInDebugger<TResult>(
719713
psCommand,
720714
executionOptions.WriteOutputToHost);
@@ -742,8 +736,6 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
742736
AddToHistory = executionOptions.AddToHistory
743737
};
744738

745-
this.logger.LogTrace("Passing to PowerShell");
746-
747739
PowerShell shell = this.PromptNest.GetPowerShell(executionOptions.IsReadLine);
748740

749741
// Due to the following PowerShell bug, we can't just assign shell.Commands to psCommand
@@ -767,6 +759,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
767759
: this.CurrentRunspace.Runspace;
768760
try
769761
{
762+
this.logger.LogDebug($"Invoking: {GetStringForPSCommand(psCommand)}");
763+
770764
// Nested PowerShell instances can't be invoked asynchronously. This occurs
771765
// in nested prompts and pipeline requests from eventing.
772766
if (shell.IsNested)
@@ -835,15 +829,11 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
835829

836830
hadErrors = true;
837831
}
838-
else
839-
{
840-
this.logger.LogTrace("Execution completed successfully");
841-
}
842832
}
843833
}
844834
catch (PSRemotingDataStructureException e)
845835
{
846-
this.logger.LogHandledException("Pipeline stopped while executing command", e);
836+
this.logger.LogHandledException("PSRemotingDataStructure exception while executing command", e);
847837
errorMessages?.Append(e.Message);
848838
}
849839
catch (PipelineStoppedException e)
@@ -1088,7 +1078,7 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10881078
.FirstOrDefault()
10891079
.ProviderPath;
10901080

1091-
this.logger.LogTrace($"Prepending working directory {workingDir} to script path {script}");
1081+
this.logger.LogDebug($"Prepending working directory {workingDir} to script path {script}");
10921082
script = Path.Combine(workingDir, script);
10931083
}
10941084
catch (System.Management.Automation.DriveNotFoundException e)
@@ -1120,7 +1110,6 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
11201110
strBld.Append(' ').Append(arguments);
11211111

11221112
var launchedScript = strBld.ToString();
1123-
this.logger.LogTrace($"Launch script is: {launchedScript}");
11241113

11251114
command.AddScript(launchedScript, false);
11261115
}
@@ -1262,15 +1251,15 @@ public void AbortExecution()
12621251
/// </param>
12631252
public void AbortExecution(bool shouldAbortDebugSession)
12641253
{
1254+
this.logger.LogTrace("Execution abort requested...");
1255+
12651256
if (this.SessionState == PowerShellContextState.Aborting
12661257
|| this.SessionState == PowerShellContextState.Disposed)
12671258
{
12681259
this.logger.LogTrace($"Execution abort requested when already aborted (SessionState = {this.SessionState})");
12691260
return;
12701261
}
12711262

1272-
this.logger.LogTrace("Execution abort requested...");
1273-
12741263
if (shouldAbortDebugSession)
12751264
{
12761265
this.ExitAllNestedPrompts();
@@ -1416,7 +1405,7 @@ private void ResumeDebugger(DebuggerResumeAction resumeAction, bool shouldWaitFo
14161405
/// </summary>
14171406
public void Close()
14181407
{
1419-
logger.LogDebug("Closing PowerShellContextService...");
1408+
logger.LogTrace("Closing PowerShellContextService...");
14201409
this.PromptNest.Dispose();
14211410
this.SessionState = PowerShellContextState.Disposed;
14221411

@@ -1854,13 +1843,7 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e
18541843
{
18551844
if (this.SessionState != PowerShellContextState.Disposed)
18561845
{
1857-
this.logger.LogTrace(
1858-
string.Format(
1859-
"Session state changed --\r\n\r\n Old state: {0}\r\n New state: {1}\r\n Result: {2}",
1860-
this.SessionState.ToString(),
1861-
e.NewSessionState.ToString(),
1862-
e.ExecutionResult));
1863-
1846+
this.logger.LogTrace($"Session state was: {SessionState}, is now: {e.NewSessionState}, result: {e.ExecutionResult}");
18641847
this.SessionState = e.NewSessionState;
18651848
this.SessionStateChanged?.Invoke(sender, e);
18661849
}
@@ -1906,8 +1889,6 @@ private void OnExecutionStatusChanged(
19061889
/// </remarks>
19071890
private void PowerShellContext_RunspaceChangedAsync(object sender, RunspaceChangedEventArgs e)
19081891
{
1909-
this.logger.LogTrace("Sending runspaceChanged notification");
1910-
19111892
_languageServer?.SendNotification(
19121893
"powerShell/runspaceChanged",
19131894
new MinifiedRunspaceDetails(e.NewRunspace));
@@ -1952,8 +1933,6 @@ public MinifiedRunspaceDetails(RunspaceDetails eventArgs)
19521933
/// <param name="e">details of the execution status change</param>
19531934
private void PowerShellContext_ExecutionStatusChangedAsync(object sender, ExecutionStatusChangedEventArgs e)
19541935
{
1955-
this.logger.LogTrace("Sending executionStatusChanged notification");
1956-
19571936
// The cancelling of the prompt (PSReadLine) causes an ExecutionStatus.Aborted to be sent after every
19581937
// actual execution (ExecutionStatus.Running) on the pipeline. We ignore that event since it's counterintuitive to
19591938
// the goal of this method which is to send updates when the pipeline is actually running something.
@@ -1973,8 +1952,6 @@ private void PowerShellContext_ExecutionStatusChangedAsync(object sender, Execut
19731952

19741953
private IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(PSCommand psCommand, bool sendOutputToHost)
19751954
{
1976-
this.logger.LogTrace($"Attempting to execute command(s) in the debugger: {GetStringForPSCommand(psCommand)}");
1977-
19781955
IEnumerable<TResult> output =
19791956
this.versionSpecificOperations.ExecuteCommandInDebugger<TResult>(
19801957
this,

0 commit comments

Comments
 (0)