@@ -33,6 +33,10 @@ public sealed class StartEditorServicesCommand : PSCmdlet
3333
3434 private HostLogger _logger ;
3535
36+ // NOTE: Ignore the suggestion to use Environment.ProcessId as it doesn't work for
37+ // .NET 4.6.2 (for Windows PowerShell), and this won't be caught in CI.
38+ private static readonly int s_currentPID = Process . GetCurrentProcess ( ) . Id ;
39+
3640 public StartEditorServicesCommand ( )
3741 {
3842 // Sets the distribution channel to "PSES" so starts can be distinguished in PS7+ telemetry
@@ -44,30 +48,30 @@ public StartEditorServicesCommand()
4448 /// <summary>
4549 /// The name of the EditorServices host to report.
4650 /// </summary>
47- [ Parameter ( Mandatory = true ) ]
51+ [ Parameter ]
4852 [ ValidateNotNullOrEmpty ]
49- public string HostName { get ; set ; }
53+ public string HostName { get ; set ; } = "PSES" ;
5054
5155 /// <summary>
5256 /// The ID to give to the host's profile.
5357 /// </summary>
54- [ Parameter ( Mandatory = true ) ]
58+ [ Parameter ]
5559 [ ValidateNotNullOrEmpty ]
56- public string HostProfileId { get ; set ; }
60+ public string HostProfileId { get ; set ; } = "PSES" ;
5761
5862 /// <summary>
5963 /// The version to report for the host.
6064 /// </summary>
61- [ Parameter ( Mandatory = true ) ]
65+ [ Parameter ]
6266 [ ValidateNotNullOrEmpty ]
63- public Version HostVersion { get ; set ; }
67+ public Version HostVersion { get ; set ; } = new Version ( 0 , 0 , 0 ) ;
6468
6569 /// <summary>
6670 /// Path to the session file to create on startup or startup failure.
6771 /// </summary>
68- [ Parameter ( Mandatory = true ) ]
72+ [ Parameter ]
6973 [ ValidateNotNullOrEmpty ]
70- public string SessionDetailsPath { get ; set ; }
74+ public string SessionDetailsPath { get ; set ; } = "PowerShellEditorServices.json" ;
7175
7276 /// <summary>
7377 /// The name of the named pipe to use for the LSP transport.
@@ -120,14 +124,16 @@ public StartEditorServicesCommand()
120124 /// </summary>
121125 [ Parameter ]
122126 [ ValidateNotNullOrEmpty ]
123- public string BundledModulesPath { get ; set ; }
127+ public string BundledModulesPath { get ; set ; } = Path . GetFullPath ( Path . Combine (
128+ Path . GetDirectoryName ( typeof ( StartEditorServicesCommand ) . Assembly . Location ) ,
129+ ".." , ".." , ".." ) ) ;
124130
125131 /// <summary>
126- /// The absolute path to the where the editor services log file should be created and logged to .
132+ /// The absolute path to the folder where logs will be saved .
127133 /// </summary>
128134 [ Parameter ]
129135 [ ValidateNotNullOrEmpty ]
130- public string LogPath { get ; set ; }
136+ public string LogPath { get ; set ; } = Path . Combine ( Path . GetTempPath ( ) , "PowerShellEditorServices" ) ;
131137
132138 /// <summary>
133139 /// The minimum log level that should be emitted.
@@ -266,34 +272,32 @@ private void StartLogging()
266272 }
267273
268274 string logDirPath = GetLogDirPath ( ) ;
269- string logPath = Path . Combine ( logDirPath , "StartEditorServices.log" ) ;
275+ string logPath = Path . Combine ( logDirPath , $ "StartEditorServices- { s_currentPID } .log") ;
270276
271- // Temp debugging sessions may try to reuse this same path,
272- // so we ensure they have a unique path
273277 if ( File . Exists ( logPath ) )
274278 {
275279 int randomInt = new Random ( ) . Next ( ) ;
276- logPath = Path . Combine ( logDirPath , $ "StartEditorServices-temp { randomInt . ToString ( "X" , CultureInfo . InvariantCulture . NumberFormat ) } .log") ;
280+ logPath = Path . Combine ( logDirPath , $ "StartEditorServices-{ s_currentPID } - { randomInt . ToString ( "X" , CultureInfo . InvariantCulture . NumberFormat ) } .log") ;
277281 }
278282
279283 StreamLogger fileLogger = StreamLogger . CreateWithNewFile ( logPath ) ;
280284 _disposableResources . Add ( fileLogger ) ;
281285 IDisposable fileLoggerUnsubscriber = _logger . Subscribe ( fileLogger ) ;
282286 fileLogger . AddUnsubscriber ( fileLoggerUnsubscriber ) ;
283287 _loggerUnsubscribers . Add ( fileLoggerUnsubscriber ) ;
284-
285288 _logger . Log ( PsesLogLevel . Diagnostic , "Logging started" ) ;
286289 }
287290
291+ // Sanitizes user input and ensures the directory is created.
288292 private string GetLogDirPath ( )
289293 {
290- string logDir = ! string . IsNullOrEmpty ( LogPath )
291- ? Path . GetDirectoryName ( LogPath )
292- : Path . GetDirectoryName ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ) ;
294+ string logDir = LogPath ;
295+ if ( string . IsNullOrEmpty ( logDir ) )
296+ {
297+ logDir = Path . Combine ( Path . GetTempPath ( ) , "PowerShellEditorServices" ) ;
298+ }
293299
294- // Ensure logDir exists
295300 Directory . CreateDirectory ( logDir ) ;
296-
297301 return logDir ;
298302 }
299303
0 commit comments