Skip to content

Commit 92ae819

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 dc078e5 commit 92ae819

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

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)
@@ -830,15 +824,11 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
830824

831825
hadErrors = true;
832826
}
833-
else
834-
{
835-
this.logger.LogTrace("Execution completed successfully");
836-
}
837827
}
838828
}
839829
catch (PSRemotingDataStructureException e)
840830
{
841-
this.logger.LogHandledException("Pipeline stopped while executing command", e);
831+
this.logger.LogHandledException("PSRemotingDataStructure exception while executing command", e);
842832
errorMessages?.Append(e.Message);
843833
}
844834
catch (PipelineStoppedException e)
@@ -1083,7 +1073,7 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10831073
.FirstOrDefault()
10841074
.ProviderPath;
10851075

1086-
this.logger.LogTrace($"Prepending working directory {workingDir} to script path {script}");
1076+
this.logger.LogDebug($"Prepending working directory {workingDir} to script path {script}");
10871077
script = Path.Combine(workingDir, script);
10881078
}
10891079
catch (System.Management.Automation.DriveNotFoundException e)
@@ -1115,7 +1105,6 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
11151105
strBld.Append(' ').Append(arguments);
11161106

11171107
var launchedScript = strBld.ToString();
1118-
this.logger.LogTrace($"Launch script is: {launchedScript}");
11191108

11201109
command.AddScript(launchedScript, false);
11211110
}
@@ -1257,15 +1246,15 @@ public void AbortExecution()
12571246
/// </param>
12581247
public void AbortExecution(bool shouldAbortDebugSession)
12591248
{
1249+
this.logger.LogTrace("Execution abort requested...");
1250+
12601251
if (this.SessionState == PowerShellContextState.Aborting
12611252
|| this.SessionState == PowerShellContextState.Disposed)
12621253
{
12631254
this.logger.LogTrace($"Execution abort requested when already aborted (SessionState = {this.SessionState})");
12641255
return;
12651256
}
12661257

1267-
this.logger.LogTrace("Execution abort requested...");
1268-
12691258
if (shouldAbortDebugSession)
12701259
{
12711260
this.ExitAllNestedPrompts();
@@ -1411,7 +1400,7 @@ private void ResumeDebugger(DebuggerResumeAction resumeAction, bool shouldWaitFo
14111400
/// </summary>
14121401
public void Close()
14131402
{
1414-
logger.LogDebug("Closing PowerShellContextService...");
1403+
logger.LogTrace("Closing PowerShellContextService...");
14151404
this.PromptNest.Dispose();
14161405
this.SessionState = PowerShellContextState.Disposed;
14171406

@@ -1849,13 +1838,7 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e
18491838
{
18501839
if (this.SessionState != PowerShellContextState.Disposed)
18511840
{
1852-
this.logger.LogTrace(
1853-
string.Format(
1854-
"Session state changed --\r\n\r\n Old state: {0}\r\n New state: {1}\r\n Result: {2}",
1855-
this.SessionState.ToString(),
1856-
e.NewSessionState.ToString(),
1857-
e.ExecutionResult));
1858-
1841+
this.logger.LogTrace($"Session state was: {SessionState}, is now: {e.NewSessionState}, result: {e.ExecutionResult}");
18591842
this.SessionState = e.NewSessionState;
18601843
this.SessionStateChanged?.Invoke(sender, e);
18611844
}
@@ -1901,8 +1884,6 @@ private void OnExecutionStatusChanged(
19011884
/// </remarks>
19021885
private void PowerShellContext_RunspaceChangedAsync(object sender, RunspaceChangedEventArgs e)
19031886
{
1904-
this.logger.LogTrace("Sending runspaceChanged notification");
1905-
19061887
_languageServer?.SendNotification(
19071888
"powerShell/runspaceChanged",
19081889
new MinifiedRunspaceDetails(e.NewRunspace));
@@ -1947,8 +1928,6 @@ public MinifiedRunspaceDetails(RunspaceDetails eventArgs)
19471928
/// <param name="e">details of the execution status change</param>
19481929
private void PowerShellContext_ExecutionStatusChangedAsync(object sender, ExecutionStatusChangedEventArgs e)
19491930
{
1950-
this.logger.LogTrace("Sending executionStatusChanged notification");
1951-
19521931
// The cancelling of the prompt (PSReadLine) causes an ExecutionStatus.Aborted to be sent after every
19531932
// actual execution (ExecutionStatus.Running) on the pipeline. We ignore that event since it's counterintuitive to
19541933
// the goal of this method which is to send updates when the pipeline is actually running something.
@@ -1968,8 +1947,6 @@ private void PowerShellContext_ExecutionStatusChangedAsync(object sender, Execut
19681947

19691948
private IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(PSCommand psCommand, bool sendOutputToHost)
19701949
{
1971-
this.logger.LogTrace($"Attempting to execute command(s) in the debugger: {GetStringForPSCommand(psCommand)}");
1972-
19731950
IEnumerable<TResult> output =
19741951
this.versionSpecificOperations.ExecuteCommandInDebugger<TResult>(
19751952
this,

0 commit comments

Comments
 (0)