@@ -33,6 +33,10 @@ public sealed class StartEditorServicesCommand : PSCmdlet
33
33
34
34
private HostLogger _logger ;
35
35
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
+
36
40
public StartEditorServicesCommand ( )
37
41
{
38
42
// Sets the distribution channel to "PSES" so starts can be distinguished in PS7+ telemetry
@@ -44,30 +48,30 @@ public StartEditorServicesCommand()
44
48
/// <summary>
45
49
/// The name of the EditorServices host to report.
46
50
/// </summary>
47
- [ Parameter ( Mandatory = true ) ]
51
+ [ Parameter ]
48
52
[ ValidateNotNullOrEmpty ]
49
- public string HostName { get ; set ; }
53
+ public string HostName { get ; set ; } = "PSES" ;
50
54
51
55
/// <summary>
52
56
/// The ID to give to the host's profile.
53
57
/// </summary>
54
- [ Parameter ( Mandatory = true ) ]
58
+ [ Parameter ]
55
59
[ ValidateNotNullOrEmpty ]
56
- public string HostProfileId { get ; set ; }
60
+ public string HostProfileId { get ; set ; } = "PSES" ;
57
61
58
62
/// <summary>
59
63
/// The version to report for the host.
60
64
/// </summary>
61
- [ Parameter ( Mandatory = true ) ]
65
+ [ Parameter ]
62
66
[ ValidateNotNullOrEmpty ]
63
- public Version HostVersion { get ; set ; }
67
+ public Version HostVersion { get ; set ; } = new Version ( 0 , 0 , 0 ) ;
64
68
65
69
/// <summary>
66
70
/// Path to the session file to create on startup or startup failure.
67
71
/// </summary>
68
- [ Parameter ( Mandatory = true ) ]
72
+ [ Parameter ]
69
73
[ ValidateNotNullOrEmpty ]
70
- public string SessionDetailsPath { get ; set ; }
74
+ public string SessionDetailsPath { get ; set ; } = "PowerShellEditorServices.json" ;
71
75
72
76
/// <summary>
73
77
/// The name of the named pipe to use for the LSP transport.
@@ -120,14 +124,16 @@ public StartEditorServicesCommand()
120
124
/// </summary>
121
125
[ Parameter ]
122
126
[ 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
+ ".." , ".." , ".." ) ) ;
124
130
125
131
/// <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 .
127
133
/// </summary>
128
134
[ Parameter ]
129
135
[ ValidateNotNullOrEmpty ]
130
- public string LogPath { get ; set ; }
136
+ public string LogPath { get ; set ; } = Path . Combine ( Path . GetTempPath ( ) , "PowerShellEditorServices" ) ;
131
137
132
138
/// <summary>
133
139
/// The minimum log level that should be emitted.
@@ -266,34 +272,32 @@ private void StartLogging()
266
272
}
267
273
268
274
string logDirPath = GetLogDirPath ( ) ;
269
- string logPath = Path . Combine ( logDirPath , "StartEditorServices.log" ) ;
275
+ string logPath = Path . Combine ( logDirPath , $ "StartEditorServices- { s_currentPID } .log") ;
270
276
271
- // Temp debugging sessions may try to reuse this same path,
272
- // so we ensure they have a unique path
273
277
if ( File . Exists ( logPath ) )
274
278
{
275
279
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") ;
277
281
}
278
282
279
283
StreamLogger fileLogger = StreamLogger . CreateWithNewFile ( logPath ) ;
280
284
_disposableResources . Add ( fileLogger ) ;
281
285
IDisposable fileLoggerUnsubscriber = _logger . Subscribe ( fileLogger ) ;
282
286
fileLogger . AddUnsubscriber ( fileLoggerUnsubscriber ) ;
283
287
_loggerUnsubscribers . Add ( fileLoggerUnsubscriber ) ;
284
-
285
288
_logger . Log ( PsesLogLevel . Diagnostic , "Logging started" ) ;
286
289
}
287
290
291
+ // Sanitizes user input and ensures the directory is created.
288
292
private string GetLogDirPath ( )
289
293
{
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
+ }
293
299
294
- // Ensure logDir exists
295
300
Directory . CreateDirectory ( logDir ) ;
296
-
297
301
return logDir ;
298
302
}
299
303
0 commit comments