Skip to content

Commit 1bd70ca

Browse files
committed
Remove almost all references to the static Logger class
This change continues the decoupling with the current codebase from the static Logger classes and transitions to using ILogger instances for writing log messages. There are still a couple uses of a static ILogger instance but they will be removed in an upcoming PR that refactors our PSHost implementations.
1 parent b6689ad commit 1bd70ca

File tree

68 files changed

+705
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+705
-419
lines changed

src/PowerShellEditorServices.Host/EditorServicesHost.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
136136

137137
string newLine = Environment.NewLine;
138138

139-
Logger.Write(
139+
this.logger.Write(
140140
LogLevel.Normal,
141141
string.Format(
142142
$"PowerShell Editor Services Host v{fileVersionInfo.FileVersion} starting (pid {Process.GetCurrentProcess().Id})..." + newLine + newLine +
@@ -166,12 +166,13 @@ public void StartLanguageService(int languageServicePort, ProfilePaths profilePa
166166
this.languageServiceListener =
167167
new TcpSocketServerListener(
168168
MessageProtocolType.LanguageServer,
169-
languageServicePort);
169+
languageServicePort,
170+
this.logger);
170171

171172
this.languageServiceListener.ClientConnect += this.OnLanguageServiceClientConnect;
172173
this.languageServiceListener.Start();
173174

174-
Logger.Write(
175+
this.logger.Write(
175176
LogLevel.Normal,
176177
string.Format(
177178
"Language service started, listening on port {0}",
@@ -191,7 +192,8 @@ private async void OnLanguageServiceClientConnect(
191192
this.languageServer =
192193
new LanguageServer(
193194
this.editorSession,
194-
serverChannel);
195+
serverChannel,
196+
this.logger);
195197

196198
await this.editorSession.PowerShellContext.ImportCommandsModule(
197199
Path.Combine(
@@ -213,12 +215,13 @@ public void StartDebugService(
213215
this.debugServiceListener =
214216
new TcpSocketServerListener(
215217
MessageProtocolType.LanguageServer,
216-
debugServicePort);
218+
debugServicePort,
219+
this.logger);
217220

218221
this.debugServiceListener.ClientConnect += OnDebugServiceClientConnect;
219222
this.debugServiceListener.Start();
220223

221-
Logger.Write(
224+
this.logger.Write(
222225
LogLevel.Normal,
223226
string.Format(
224227
"Debug service started, listening on port {0}",
@@ -233,7 +236,8 @@ private async void OnDebugServiceClientConnect(object sender, TcpSocketServerCha
233236
new DebugAdapter(
234237
this.editorSession,
235238
serverChannel,
236-
false);
239+
false,
240+
this.logger);
237241
}
238242
else
239243
{
@@ -247,13 +251,14 @@ private async void OnDebugServiceClientConnect(object sender, TcpSocketServerCha
247251
new DebugAdapter(
248252
debugSession,
249253
serverChannel,
250-
true);
254+
true,
255+
this.logger);
251256
}
252257

253258
this.debugAdapter.SessionEnded +=
254259
(obj, args) =>
255260
{
256-
Logger.Write(
261+
this.logger.Write(
257262
LogLevel.Normal,
258263
"Previous debug session ended, restarting debug service listener...");
259264

@@ -294,8 +299,8 @@ private EditorSession CreateSession(
294299
ProfilePaths profilePaths,
295300
bool enableConsoleRepl)
296301
{
297-
EditorSession editorSession = new EditorSession();
298-
PowerShellContext powerShellContext = new PowerShellContext();
302+
EditorSession editorSession = new EditorSession(this.logger);
303+
PowerShellContext powerShellContext = new PowerShellContext(this.logger);
299304

300305
ConsoleServicePSHost psHost =
301306
new ConsoleServicePSHost(
@@ -318,8 +323,8 @@ private EditorSession CreateDebugSession(
318323
ProfilePaths profilePaths,
319324
IEditorOperations editorOperations)
320325
{
321-
EditorSession editorSession = new EditorSession();
322-
PowerShellContext powerShellContext = new PowerShellContext();
326+
EditorSession editorSession = new EditorSession(this.logger);
327+
PowerShellContext powerShellContext = new PowerShellContext(this.logger);
323328

324329
ConsoleServicePSHost psHost =
325330
new ConsoleServicePSHost(
@@ -339,12 +344,12 @@ private EditorSession CreateDebugSession(
339344
}
340345

341346
#if !CoreCLR
342-
static void CurrentDomain_UnhandledException(
347+
private void CurrentDomain_UnhandledException(
343348
object sender,
344349
UnhandledExceptionEventArgs e)
345350
{
346351
// Log the exception
347-
Logger.Write(
352+
this.logger.Write(
348353
LogLevel.Error,
349354
string.Format(
350355
"FATAL UNHANDLED EXCEPTION:\r\n\r\n{0}",

src/PowerShellEditorServices.Protocol/Client/DebugAdapterClientBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
99
using System.Threading.Tasks;
10+
using Microsoft.PowerShell.EditorServices.Utility;
1011

1112
namespace Microsoft.PowerShell.EditorServices.Protocol.Client
1213
{
1314
public class DebugAdapterClient : ProtocolEndpoint
1415
{
15-
public DebugAdapterClient(ChannelBase clientChannel)
16+
public DebugAdapterClient(ChannelBase clientChannel, ILogger logger)
1617
: base(
1718
clientChannel,
18-
new MessageDispatcher(),
19-
MessageProtocolType.DebugAdapter)
19+
new MessageDispatcher(logger),
20+
MessageProtocolType.DebugAdapter,
21+
logger)
2022
{
2123
}
2224

src/PowerShellEditorServices.Protocol/Client/LanguageClientBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
99
using System.Threading.Tasks;
10+
using Microsoft.PowerShell.EditorServices.Utility;
1011

1112
namespace Microsoft.PowerShell.EditorServices.Protocol.Client
1213
{
@@ -20,11 +21,12 @@ public abstract class LanguageClientBase : ProtocolEndpoint
2021
/// specified channel for communication.
2122
/// </summary>
2223
/// <param name="clientChannel">The channel to use for communication with the server.</param>
23-
public LanguageClientBase(ChannelBase clientChannel)
24+
public LanguageClientBase(ChannelBase clientChannel, ILogger logger)
2425
: base(
2526
clientChannel,
26-
new MessageDispatcher(),
27-
MessageProtocolType.LanguageServer)
27+
new MessageDispatcher(logger),
28+
MessageProtocolType.LanguageServer,
29+
logger)
2830
{
2931
}
3032

src/PowerShellEditorServices.Protocol/Client/LanguageServiceClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Linq;
1212
using System.Threading.Tasks;
13+
using Microsoft.PowerShell.EditorServices.Utility;
1314

1415
namespace Microsoft.PowerShell.EditorServices.Protocol.Client
1516
{
@@ -18,8 +19,8 @@ public class LanguageServiceClient : LanguageClientBase
1819
private Dictionary<string, ScriptFileMarker[]> cachedDiagnostics =
1920
new Dictionary<string, ScriptFileMarker[]>();
2021

21-
public LanguageServiceClient(ChannelBase clientChannel)
22-
: base(clientChannel)
22+
public LanguageServiceClient(ChannelBase clientChannel, ILogger logger)
23+
: base(clientChannel, logger)
2324
{
2425
}
2526

src/PowerShellEditorServices.Protocol/MessageProtocol/Channel/NamedPipeClientChannel.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
using System;
77
using System.IO.Pipes;
88
using System.Threading.Tasks;
9+
using Microsoft.PowerShell.EditorServices.Utility;
910

1011
namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
1112
{
1213
public class NamedPipeClientChannel : ChannelBase
1314
{
15+
private ILogger logger;
1416
private NamedPipeClientStream pipeClient;
1517

16-
public NamedPipeClientChannel(NamedPipeClientStream pipeClient)
18+
public NamedPipeClientChannel(
19+
NamedPipeClientStream pipeClient,
20+
ILogger logger)
1721
{
1822
this.pipeClient = pipeClient;
1923
}
@@ -23,12 +27,14 @@ protected override void Initialize(IMessageSerializer messageSerializer)
2327
this.MessageReader =
2428
new MessageReader(
2529
this.pipeClient,
26-
messageSerializer);
30+
messageSerializer,
31+
this.logger);
2732

2833
this.MessageWriter =
2934
new MessageWriter(
3035
this.pipeClient,
31-
messageSerializer);
36+
messageSerializer,
37+
this.logger);
3238
}
3339

3440
protected override void Shutdown()
@@ -41,7 +47,8 @@ protected override void Shutdown()
4147

4248
public static async Task<NamedPipeClientChannel> Connect(
4349
string pipeName,
44-
MessageProtocolType messageProtocolType)
50+
MessageProtocolType messageProtocolType,
51+
ILogger logger)
4552
{
4653
var pipeClient =
4754
new NamedPipeClientStream(
@@ -68,7 +75,7 @@ public static async Task<NamedPipeClientChannel> Connect(
6875
}
6976
}
7077
#endif
71-
var clientChannel = new NamedPipeClientChannel(pipeClient);
78+
var clientChannel = new NamedPipeClientChannel(pipeClient, logger);
7279
clientChannel.Start(messageProtocolType);
7380

7481
return clientChannel;

src/PowerShellEditorServices.Protocol/MessageProtocol/Channel/NamedPipeServerChannel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,30 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
1010
{
1111
public class NamedPipeServerChannel : ChannelBase
1212
{
13+
private ILogger logger;
1314
private NamedPipeServerStream pipeServer;
1415

15-
public NamedPipeServerChannel(NamedPipeServerStream pipeServer)
16+
public NamedPipeServerChannel(
17+
NamedPipeServerStream pipeServer,
18+
ILogger logger)
1619
{
1720
this.pipeServer = pipeServer;
21+
this.logger = logger;
1822
}
1923

2024
protected override void Initialize(IMessageSerializer messageSerializer)
2125
{
2226
this.MessageReader =
2327
new MessageReader(
2428
this.pipeServer,
25-
messageSerializer);
29+
messageSerializer,
30+
this.logger);
2631

2732
this.MessageWriter =
2833
new MessageWriter(
2934
this.pipeServer,
30-
messageSerializer);
35+
messageSerializer,
36+
this.logger);
3137
}
3238

3339
protected override void Shutdown()

src/PowerShellEditorServices.Protocol/MessageProtocol/Channel/NamedPipeServerListener.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
1313
{
1414
public class NamedPipeServerListener : ServerListenerBase<NamedPipeServerChannel>
1515
{
16+
private ILogger logger;
1617
private string pipeName;
1718
private NamedPipeServerStream pipeServer;
1819

1920
public NamedPipeServerListener(
2021
MessageProtocolType messageProtocolType,
21-
string pipeName)
22+
string pipeName,
23+
ILogger logger)
2224
: base(messageProtocolType)
2325
{
26+
this.logger = logger;
2427
this.pipeName = pipeName;
2528
}
2629

@@ -38,7 +41,7 @@ public override void Start()
3841
}
3942
catch (IOException e)
4043
{
41-
Logger.Write(
44+
this.logger.Write(
4245
LogLevel.Verbose,
4346
"Named pipe server failed to start due to exception:\r\n\r\n" + e.Message);
4447

@@ -50,11 +53,11 @@ public override void Stop()
5053
{
5154
if (this.pipeServer != null)
5255
{
53-
Logger.Write(LogLevel.Verbose, "Named pipe server shutting down...");
56+
this.logger.Write(LogLevel.Verbose, "Named pipe server shutting down...");
5457

5558
this.pipeServer.Dispose();
5659

57-
Logger.Write(LogLevel.Verbose, "Named pipe server has been disposed.");
60+
this.logger.Write(LogLevel.Verbose, "Named pipe server has been disposed.");
5861
}
5962
}
6063

@@ -69,16 +72,17 @@ private void ListenForConnection()
6972
await this.pipeServer.WaitForConnectionAsync();
7073
#else
7174
await Task.Factory.FromAsync(
72-
this.pipeServer.BeginWaitForConnection,
75+
this.pipeServer.BeginWaitForConnection,
7376
this.pipeServer.EndWaitForConnection, null);
7477
#endif
7578
this.OnClientConnect(
7679
new NamedPipeServerChannel(
77-
this.pipeServer));
80+
this.pipeServer,
81+
this.logger));
7882
}
7983
catch (Exception e)
8084
{
81-
Logger.WriteException(
85+
this.logger.WriteException(
8286
"An unhandled exception occurred while listening for a named pipe client connection",
8387
e);
8488

src/PowerShellEditorServices.Protocol/MessageProtocol/Channel/StdioClientChannel.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.IO;
88
using System.Text;
9+
using Microsoft.PowerShell.EditorServices.Utility;
910

1011
namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
1112
{
@@ -19,6 +20,7 @@ public class StdioClientChannel : ChannelBase
1920
private string serviceProcessPath;
2021
private string serviceProcessArguments;
2122

23+
private ILogger logger;
2224
private Stream inputStream;
2325
private Stream outputStream;
2426
private Process serviceProcess;
@@ -35,8 +37,10 @@ public class StdioClientChannel : ChannelBase
3537
/// <param name="serverProcessArguments">Optional arguments to pass to the service process executable.</param>
3638
public StdioClientChannel(
3739
string serverProcessPath,
40+
ILogger logger,
3841
params string[] serverProcessArguments)
3942
{
43+
this.logger = logger;
4044
this.serviceProcessPath = serverProcessPath;
4145

4246
if (serverProcessArguments != null)
@@ -48,11 +52,6 @@ public StdioClientChannel(
4852
}
4953
}
5054

51-
public StdioClientChannel(Process serviceProcess)
52-
{
53-
this.serviceProcess = serviceProcess;
54-
}
55-
5655
protected override void Initialize(IMessageSerializer messageSerializer)
5756
{
5857
this.serviceProcess = new Process
@@ -84,12 +83,14 @@ protected override void Initialize(IMessageSerializer messageSerializer)
8483
this.MessageReader =
8584
new MessageReader(
8685
this.inputStream,
87-
messageSerializer);
86+
messageSerializer,
87+
this.logger);
8888

8989
this.MessageWriter =
9090
new MessageWriter(
9191
this.outputStream,
92-
messageSerializer);
92+
messageSerializer,
93+
this.logger);
9394
}
9495

9596
protected override void Shutdown()

0 commit comments

Comments
 (0)