@@ -174,6 +174,7 @@ public PowerShellContextService(
174
174
OmniSharp . Extensions . LanguageServer . Protocol . Server . ILanguageServerFacade languageServer ,
175
175
bool isPSReadLineEnabled )
176
176
{
177
+ logger . LogTrace ( "Instantiating PowerShellContextService and adding event handlers" ) ;
177
178
_languageServer = languageServer ;
178
179
this . logger = logger ;
179
180
this . isPSReadLineEnabled = isPSReadLineEnabled ;
@@ -212,9 +213,9 @@ public static PowerShellContextService Create(
212
213
hostUserInterface ,
213
214
logger ) ;
214
215
216
+ logger . LogTrace ( "Creating initial PowerShell runspace" ) ;
215
217
Runspace initialRunspace = PowerShellContextService . CreateRunspace ( psHost , hostStartupInfo . LanguageMode ) ;
216
218
powerShellContext . Initialize ( hostStartupInfo . ProfilePaths , initialRunspace , true , hostUserInterface ) ;
217
-
218
219
powerShellContext . ImportCommandsModuleAsync ( ) ;
219
220
220
221
// TODO: This can be moved to the point after the $psEditor object
@@ -325,6 +326,7 @@ public void Initialize(
325
326
IHostOutput consoleHost )
326
327
{
327
328
Validate . IsNotNull ( "initialRunspace" , initialRunspace ) ;
329
+ this . logger . LogTrace ( $ "Initializing PowerShell context with runspace { initialRunspace . Name } ") ;
328
330
329
331
this . ownsInitialRunspace = ownsInitialRunspace ;
330
332
this . SessionState = PowerShellContextState . NotStarted ;
@@ -351,11 +353,7 @@ public void Initialize(
351
353
this . CurrentRunspace = this . initialRunspace ;
352
354
353
355
// 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 ) ) ;
356
+ this . logger . LogInformation ( $ "PowerShell Version: { this . LocalPowerShellVersion . Version } , Edition: { this . LocalPowerShellVersion . Edition } ") ;
359
357
360
358
Version powerShellVersion = this . LocalPowerShellVersion . Version ;
361
359
if ( powerShellVersion >= new Version ( 5 , 0 ) )
@@ -441,6 +439,8 @@ public void Initialize(
441
439
442
440
public Task ImportCommandsModuleAsync ( string path )
443
441
{
442
+ this . logger . LogTrace ( $ "Importing PowershellEditorServices commands from { path } ") ;
443
+
444
444
PSCommand importCommand = new PSCommand ( )
445
445
. AddCommand ( "Import-Module" )
446
446
. AddArgument ( path ) ;
@@ -463,6 +463,7 @@ private static bool CheckIfRunspaceNeedsEventHandlers(RunspaceDetails runspaceDe
463
463
464
464
private void ConfigureRunspace ( RunspaceDetails runspaceDetails )
465
465
{
466
+ this . logger . LogTrace ( "Configuring Runspace" ) ;
466
467
runspaceDetails . Runspace . StateChanged += this . HandleRunspaceStateChanged ;
467
468
if ( runspaceDetails . Runspace . Debugger != null )
468
469
{
@@ -475,6 +476,7 @@ private void ConfigureRunspace(RunspaceDetails runspaceDetails)
475
476
476
477
private void CleanupRunspace ( RunspaceDetails runspaceDetails )
477
478
{
479
+ this . logger . LogTrace ( "Cleaning Up Runspace" ) ;
478
480
runspaceDetails . Runspace . StateChanged -= this . HandleRunspaceStateChanged ;
479
481
if ( runspaceDetails . Runspace . Debugger != null )
480
482
{
@@ -596,6 +598,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
596
598
Validate . IsNotNull ( nameof ( psCommand ) , psCommand ) ;
597
599
Validate . IsNotNull ( nameof ( executionOptions ) , executionOptions ) ;
598
600
601
+ this . logger . LogTrace ( $ "Attempting to execute command(s): { GetStringForPSCommand ( psCommand ) } ") ;
602
+
599
603
// Add history to PSReadLine before cancelling, otherwise it will be restored as the
600
604
// cancelled prompt when it's called again.
601
605
if ( executionOptions . AddToHistory )
@@ -629,7 +633,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
629
633
this . ShouldExecuteWithEventing ( executionOptions ) ||
630
634
( PromptNest . IsRemote && executionOptions . IsReadLine ) ) )
631
635
{
632
- this . logger . LogTrace ( "Passing command execution to pipeline thread. " ) ;
636
+ this . logger . LogTrace ( "Passing command execution to pipeline thread" ) ;
633
637
634
638
if ( shouldCancelReadLine && PromptNest . IsReadLineBusy ( ) )
635
639
{
@@ -714,8 +718,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
714
718
}
715
719
catch ( Exception e )
716
720
{
717
- logger . LogError (
718
- "Exception occurred while executing debugger command:\r \n \r \n " + e . ToString ( ) ) ;
721
+ this . logger . LogException ( "Exception occurred while executing debugger command" , e ) ;
719
722
}
720
723
finally
721
724
{
@@ -736,11 +739,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
736
739
AddToHistory = executionOptions . AddToHistory
737
740
} ;
738
741
739
- this . logger . LogTrace (
740
- string . Format (
741
- "Attempting to execute command(s):\r \n \r \n {0}" ,
742
- GetStringForPSCommand ( psCommand ) ) ) ;
743
-
742
+ this . logger . LogTrace ( "Passing to PowerShell" ) ;
744
743
745
744
PowerShell shell = this . PromptNest . GetPowerShell ( executionOptions . IsReadLine ) ;
746
745
@@ -818,29 +817,23 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
818
817
}
819
818
else
820
819
{
821
- this . logger . LogTrace (
822
- "Execution completed successfully." ) ;
820
+ this . logger . LogTrace ( "Execution completed successfully" ) ;
823
821
}
824
822
}
825
823
}
826
824
catch ( PSRemotingDataStructureException e )
827
825
{
828
- this . logger . LogError (
829
- "Pipeline stopped while executing command:\r \n \r \n " + e . ToString ( ) ) ;
830
-
826
+ this . logger . LogHandledException ( "Pipeline stopped while executing command" , e ) ;
831
827
errorMessages ? . Append ( e . Message ) ;
832
828
}
833
829
catch ( PipelineStoppedException e )
834
830
{
835
- this . logger . LogError (
836
- "Pipeline stopped while executing command:\r \n \r \n " + e . ToString ( ) ) ;
837
-
831
+ this . logger . LogHandledException ( "Pipeline stopped while executing command" , e ) ;
838
832
errorMessages ? . Append ( e . Message ) ;
839
833
}
840
834
catch ( RuntimeException e )
841
835
{
842
- this . logger . LogWarning (
843
- "Runtime exception occurred while executing command:\r \n \r \n " + e . ToString ( ) ) ;
836
+ this . logger . LogHandledException ( "Runtime exception occurred while executing command" , e ) ;
844
837
845
838
hadErrors = true ;
846
839
errorMessages ? . Append ( e . Message ) ;
@@ -1078,8 +1071,7 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
1078
1071
}
1079
1072
catch ( System . Management . Automation . DriveNotFoundException e )
1080
1073
{
1081
- this . logger . LogError (
1082
- "Could not determine current filesystem location:\r \n \r \n " + e . ToString ( ) ) ;
1074
+ this . logger . LogHandledException ( "Could not determine current filesystem location" , e ) ;
1083
1075
}
1084
1076
1085
1077
var strBld = new StringBuilder ( ) ;
@@ -1250,9 +1242,7 @@ public void AbortExecution(bool shouldAbortDebugSession)
1250
1242
if ( this . SessionState == PowerShellContextState . Aborting
1251
1243
|| this . SessionState == PowerShellContextState . Disposed )
1252
1244
{
1253
- this . logger . LogTrace (
1254
- string . Format (
1255
- $ "Execution abort requested when already aborted (SessionState = { this . SessionState } )") ) ;
1245
+ this . logger . LogTrace ( $ "Execution abort requested when already aborted (SessionState = { this . SessionState } )") ;
1256
1246
return ;
1257
1247
}
1258
1248
@@ -1539,8 +1529,8 @@ private void CloseRunspace(RunspaceDetails runspaceDetails)
1539
1529
1540
1530
if ( exitException != null )
1541
1531
{
1542
- this . logger . LogError (
1543
- $ "Caught { exitException . GetType ( ) . Name } while exiting { runspaceDetails . Location } runspace: \r \n { exitException . ToString ( ) } " ) ;
1532
+ this . logger . LogHandledException (
1533
+ $ "Caught { exitException . GetType ( ) . Name } while exiting { runspaceDetails . Location } runspace" , exitException ) ;
1544
1534
}
1545
1535
}
1546
1536
}
@@ -1582,6 +1572,8 @@ internal bool IsCurrentRunspaceOutOfProcess()
1582
1572
/// </summary>
1583
1573
internal void EnterNestedPrompt ( )
1584
1574
{
1575
+ this . logger . LogTrace ( "Entering nested prompt" ) ;
1576
+
1585
1577
if ( this . IsCurrentRunspaceOutOfProcess ( ) )
1586
1578
{
1587
1579
throw new NotSupportedException ( ) ;
@@ -1884,6 +1876,11 @@ private void OnExecutionStatusChanged(
1884
1876
hadErrors ) ) ;
1885
1877
}
1886
1878
1879
+ /// <remarks>
1880
+ /// TODO: This should somehow check if the server has actually started because we are
1881
+ /// currently sending this notification before it has initialized, which is not allowed.
1882
+ /// This might be the cause of our deadlock!
1883
+ /// </remarks>
1887
1884
private void PowerShellContext_RunspaceChangedAsync ( object sender , RunspaceChangedEventArgs e )
1888
1885
{
1889
1886
_languageServer ? . SendNotification (
@@ -1922,6 +1919,10 @@ public MinifiedRunspaceDetails(RunspaceDetails eventArgs)
1922
1919
/// <summary>
1923
1920
/// Event hook on the PowerShell context to listen for changes in script execution status
1924
1921
/// </summary>
1922
+ /// <remarks>
1923
+ /// TODO: This should somehow check if the server has actually started because we are
1924
+ /// currently sending this notification before it has initialized, which is not allowed.
1925
+ /// </remarks>
1925
1926
/// <param name="sender">the PowerShell context sending the execution event</param>
1926
1927
/// <param name="e">details of the execution status change</param>
1927
1928
private void PowerShellContext_ExecutionStatusChangedAsync ( object sender , ExecutionStatusChangedEventArgs e )
@@ -1945,10 +1946,7 @@ private void PowerShellContext_ExecutionStatusChangedAsync(object sender, Execut
1945
1946
1946
1947
private IEnumerable < TResult > ExecuteCommandInDebugger < TResult > ( PSCommand psCommand , bool sendOutputToHost )
1947
1948
{
1948
- this . logger . LogTrace (
1949
- string . Format (
1950
- "Attempting to execute command(s) in the debugger:\r \n \r \n {0}" ,
1951
- GetStringForPSCommand ( psCommand ) ) ) ;
1949
+ this . logger . LogTrace ( $ "Attempting to execute command(s)a in the debugger: { GetStringForPSCommand ( psCommand ) } ") ;
1952
1950
1953
1951
IEnumerable < TResult > output =
1954
1952
this . versionSpecificOperations . ExecuteCommandInDebugger < TResult > (
@@ -2144,6 +2142,7 @@ private static string GetStringForPSCommand(PSCommand psCommand)
2144
2142
2145
2143
private void SetExecutionPolicy ( )
2146
2144
{
2145
+ this . logger . LogTrace ( "Setting Execution Policy" ) ;
2147
2146
// We want to get the list hierarchy of execution policies
2148
2147
// Calling the cmdlet is the simplest way to do that
2149
2148
IReadOnlyList < PSObject > policies = this . powerShell
@@ -2188,7 +2187,7 @@ private void SetExecutionPolicy()
2188
2187
}
2189
2188
2190
2189
// Finally set the inherited execution policy
2191
- this . logger . LogTrace ( "Setting execution policy to {Policy}" , policyToSet ) ;
2190
+ this . logger . LogTrace ( $ "Setting execution policy to { policyToSet } " ) ;
2192
2191
try
2193
2192
{
2194
2193
this . powerShell
@@ -2200,7 +2199,8 @@ private void SetExecutionPolicy()
2200
2199
}
2201
2200
catch ( CmdletInvocationException e )
2202
2201
{
2203
- this . logger . LogError ( e , "Error occurred calling 'Set-ExecutionPolicy -Scope Process -ExecutionPolicy {Policy} -Force'" , policyToSet ) ;
2202
+ this . logger . LogHandledException (
2203
+ $ "Error occurred calling 'Set-ExecutionPolicy -Scope Process -ExecutionPolicy { policyToSet } -Force'", e ) ;
2204
2204
}
2205
2205
finally
2206
2206
{
@@ -2221,13 +2221,11 @@ private SessionDetails GetSessionDetails(Func<PSCommand, PSObject> invokeAction)
2221
2221
}
2222
2222
catch ( RuntimeException e )
2223
2223
{
2224
- this . logger . LogTrace (
2225
- "Runtime exception occurred while gathering runspace info:\r \n \r \n " + e . ToString ( ) ) ;
2224
+ this . logger . LogHandledException ( "Runtime exception occurred while gathering runspace info" , e ) ;
2226
2225
}
2227
2226
catch ( ArgumentNullException )
2228
2227
{
2229
- this . logger . LogError (
2230
- "Could not retrieve session details but no exception was thrown." ) ;
2228
+ this . logger . LogError ( "Could not retrieve session details but no exception was thrown." ) ;
2231
2229
}
2232
2230
2233
2231
// TODO: Return a harmless object if necessary
@@ -2320,9 +2318,7 @@ private void SetProfileVariableInCurrentRunspace(ProfilePathInfo profilePaths)
2320
2318
profilePaths . CurrentUserCurrentHost ) ) ;
2321
2319
2322
2320
this . logger . LogTrace (
2323
- string . Format (
2324
- "Setting $profile variable in runspace. Current user host profile path: {0}" ,
2325
- profilePaths . CurrentUserCurrentHost ) ) ;
2321
+ $ "Setting $profile variable in runspace. Current user host profile path: { profilePaths . CurrentUserCurrentHost } ") ;
2326
2322
2327
2323
// Set the variable in the runspace
2328
2324
this . powerShell . Commands . Clear ( ) ;
@@ -2545,6 +2541,7 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
2545
2541
else
2546
2542
{
2547
2543
// TODO: How to handle this?
2544
+ this . logger . LogError ( $ "Unhandled TaskIndex: { taskIndex } ") ;
2548
2545
}
2549
2546
}
2550
2547
0 commit comments