Skip to content

Remove static Logger API in favor of ILogger instance usage #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/PowerShellEditorServices.Host/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class EditorServicesHost
{
#region Private Fields

private ILogger logger;
private bool enableConsoleRepl;
private HostDetails hostDetails;
private ProfilePaths profilePaths;
Expand Down Expand Up @@ -114,7 +115,8 @@ public EditorServicesHost(
/// <param name="logLevel">The minimum level of log messages to be written.</param>
public void StartLogging(string logFilePath, LogLevel logLevel)
{
Logger.Initialize(logFilePath, logLevel);
this.logger = new FileLogger(logFilePath, logLevel);
Logger.Initialize(this.logger);

#if CoreCLR
FileVersionInfo fileVersionInfo =
Expand All @@ -134,7 +136,7 @@ public void StartLogging(string logFilePath, LogLevel logLevel)

string newLine = Environment.NewLine;

Logger.Write(
this.logger.Write(
LogLevel.Normal,
string.Format(
$"PowerShell Editor Services Host v{fileVersionInfo.FileVersion} starting (pid {Process.GetCurrentProcess().Id})..." + newLine + newLine +
Expand Down Expand Up @@ -164,12 +166,13 @@ public void StartLanguageService(int languageServicePort, ProfilePaths profilePa
this.languageServiceListener =
new TcpSocketServerListener(
MessageProtocolType.LanguageServer,
languageServicePort);
languageServicePort,
this.logger);

this.languageServiceListener.ClientConnect += this.OnLanguageServiceClientConnect;
this.languageServiceListener.Start();

Logger.Write(
this.logger.Write(
LogLevel.Normal,
string.Format(
"Language service started, listening on port {0}",
Expand All @@ -189,7 +192,8 @@ private async void OnLanguageServiceClientConnect(
this.languageServer =
new LanguageServer(
this.editorSession,
serverChannel);
serverChannel,
this.logger);

await this.editorSession.PowerShellContext.ImportCommandsModule(
Path.Combine(
Expand All @@ -211,12 +215,13 @@ public void StartDebugService(
this.debugServiceListener =
new TcpSocketServerListener(
MessageProtocolType.LanguageServer,
debugServicePort);
debugServicePort,
this.logger);

this.debugServiceListener.ClientConnect += OnDebugServiceClientConnect;
this.debugServiceListener.Start();

Logger.Write(
this.logger.Write(
LogLevel.Normal,
string.Format(
"Debug service started, listening on port {0}",
Expand All @@ -231,7 +236,8 @@ private async void OnDebugServiceClientConnect(object sender, TcpSocketServerCha
new DebugAdapter(
this.editorSession,
serverChannel,
false);
false,
this.logger);
}
else
{
Expand All @@ -245,13 +251,14 @@ private async void OnDebugServiceClientConnect(object sender, TcpSocketServerCha
new DebugAdapter(
debugSession,
serverChannel,
true);
true,
this.logger);
}

this.debugAdapter.SessionEnded +=
(obj, args) =>
{
Logger.Write(
this.logger.Write(
LogLevel.Normal,
"Previous debug session ended, restarting debug service listener...");

Expand Down Expand Up @@ -292,8 +299,8 @@ private EditorSession CreateSession(
ProfilePaths profilePaths,
bool enableConsoleRepl)
{
EditorSession editorSession = new EditorSession();
PowerShellContext powerShellContext = new PowerShellContext();
EditorSession editorSession = new EditorSession(this.logger);
PowerShellContext powerShellContext = new PowerShellContext(this.logger);

ConsoleServicePSHost psHost =
new ConsoleServicePSHost(
Expand All @@ -316,8 +323,8 @@ private EditorSession CreateDebugSession(
ProfilePaths profilePaths,
IEditorOperations editorOperations)
{
EditorSession editorSession = new EditorSession();
PowerShellContext powerShellContext = new PowerShellContext();
EditorSession editorSession = new EditorSession(this.logger);
PowerShellContext powerShellContext = new PowerShellContext(this.logger);

ConsoleServicePSHost psHost =
new ConsoleServicePSHost(
Expand All @@ -337,12 +344,12 @@ private EditorSession CreateDebugSession(
}

#if !CoreCLR
static void CurrentDomain_UnhandledException(
private void CurrentDomain_UnhandledException(
object sender,
UnhandledExceptionEventArgs e)
{
// Log the exception
Logger.Write(
this.logger.Write(
LogLevel.Error,
string.Format(
"FATAL UNHANDLED EXCEPTION:\r\n\r\n{0}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Protocol.Client
{
public class DebugAdapterClient : ProtocolEndpoint
{
public DebugAdapterClient(ChannelBase clientChannel)
public DebugAdapterClient(ChannelBase clientChannel, ILogger logger)
: base(
clientChannel,
new MessageDispatcher(),
MessageProtocolType.DebugAdapter)
new MessageDispatcher(logger),
MessageProtocolType.DebugAdapter,
logger)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Protocol.Client
{
Expand All @@ -20,11 +21,12 @@ public abstract class LanguageClientBase : ProtocolEndpoint
/// specified channel for communication.
/// </summary>
/// <param name="clientChannel">The channel to use for communication with the server.</param>
public LanguageClientBase(ChannelBase clientChannel)
public LanguageClientBase(ChannelBase clientChannel, ILogger logger)
: base(
clientChannel,
new MessageDispatcher(),
MessageProtocolType.LanguageServer)
new MessageDispatcher(logger),
MessageProtocolType.LanguageServer,
logger)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Utility;

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

public LanguageServiceClient(ChannelBase clientChannel)
: base(clientChannel)
public LanguageServiceClient(ChannelBase clientChannel, ILogger logger)
: base(clientChannel, logger)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@
using System;
using System.IO.Pipes;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
{
public class NamedPipeClientChannel : ChannelBase
{
private ILogger logger;
private NamedPipeClientStream pipeClient;

public NamedPipeClientChannel(NamedPipeClientStream pipeClient)
public NamedPipeClientChannel(
NamedPipeClientStream pipeClient,
ILogger logger)
{
this.pipeClient = pipeClient;
this.logger = logger;
}

protected override void Initialize(IMessageSerializer messageSerializer)
{
this.MessageReader =
new MessageReader(
this.pipeClient,
messageSerializer);
messageSerializer,
this.logger);

this.MessageWriter =
new MessageWriter(
this.pipeClient,
messageSerializer);
messageSerializer,
this.logger);
}

protected override void Shutdown()
Expand All @@ -41,7 +48,8 @@ protected override void Shutdown()

public static async Task<NamedPipeClientChannel> Connect(
string pipeName,
MessageProtocolType messageProtocolType)
MessageProtocolType messageProtocolType,
ILogger logger)
{
var pipeClient =
new NamedPipeClientStream(
Expand All @@ -68,7 +76,7 @@ public static async Task<NamedPipeClientChannel> Connect(
}
}
#endif
var clientChannel = new NamedPipeClientChannel(pipeClient);
var clientChannel = new NamedPipeClientChannel(pipeClient, logger);
clientChannel.Start(messageProtocolType);

return clientChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
{
public class NamedPipeServerChannel : ChannelBase
{
private ILogger logger;
private NamedPipeServerStream pipeServer;

public NamedPipeServerChannel(NamedPipeServerStream pipeServer)
public NamedPipeServerChannel(
NamedPipeServerStream pipeServer,
ILogger logger)
{
this.pipeServer = pipeServer;
this.logger = logger;
}

protected override void Initialize(IMessageSerializer messageSerializer)
{
this.MessageReader =
new MessageReader(
this.pipeServer,
messageSerializer);
messageSerializer,
this.logger);

this.MessageWriter =
new MessageWriter(
this.pipeServer,
messageSerializer);
messageSerializer,
this.logger);
}

protected override void Shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel
{
public class NamedPipeServerListener : ServerListenerBase<NamedPipeServerChannel>
{
private ILogger logger;
private string pipeName;
private NamedPipeServerStream pipeServer;

public NamedPipeServerListener(
MessageProtocolType messageProtocolType,
string pipeName)
string pipeName,
ILogger logger)
: base(messageProtocolType)
{
this.logger = logger;
this.pipeName = pipeName;
}

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

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

this.pipeServer.Dispose();

Logger.Write(LogLevel.Verbose, "Named pipe server has been disposed.");
this.logger.Write(LogLevel.Verbose, "Named pipe server has been disposed.");
}
}

Expand All @@ -69,16 +72,17 @@ private void ListenForConnection()
await this.pipeServer.WaitForConnectionAsync();
#else
await Task.Factory.FromAsync(
this.pipeServer.BeginWaitForConnection,
this.pipeServer.BeginWaitForConnection,
this.pipeServer.EndWaitForConnection, null);
#endif
this.OnClientConnect(
new NamedPipeServerChannel(
this.pipeServer));
this.pipeServer,
this.logger));
}
catch (Exception e)
{
Logger.WriteException(
this.logger.WriteException(
"An unhandled exception occurred while listening for a named pipe client connection",
e);

Expand Down
Loading