Skip to content

Commit 29a560f

Browse files
committed
Improve logging for host process tests
This change adds a new /logPath parameter to the host process which is used to provide unique log paths for host process tests. These new log paths will help diagnose issues with individual tests, especially when running on AppVeyor.
1 parent 9c90e98 commit 29a560f

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

src/PowerShellEditorServices.Host/Program.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ static void Main(string[] args)
4040
}
4141
#endif
4242

43+
string logPath = null;
44+
string logPathArgument =
45+
args.FirstOrDefault(
46+
arg =>
47+
arg.StartsWith(
48+
"/logPath:",
49+
StringComparison.InvariantCultureIgnoreCase));
50+
51+
if (!string.IsNullOrEmpty(logPathArgument))
52+
{
53+
logPath = logPathArgument.Substring(9).Trim('"');
54+
}
55+
4356
bool runDebugAdapter =
4457
args.Any(
4558
arg =>
@@ -51,34 +64,29 @@ static void Main(string[] args)
5164
// Catch unhandled exceptions for logging purposes
5265
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
5366

54-
if (runDebugAdapter)
55-
{
56-
// TODO: Remove this behavior in the near future --
57-
// Create the debug service log in a separate file
58-
// so that there isn't a conflict with the default
59-
// log file.
60-
Logger.Initialize("DebugAdapter.log", LogLevel.Verbose);
61-
}
62-
else
63-
{
64-
// Initialize the logger
65-
// TODO: Set the level based on command line parameter
66-
Logger.Initialize(minimumLogLevel: LogLevel.Verbose);
67-
}
68-
69-
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host started!");
70-
7167
ProtocolServer server = null;
7268
if (runDebugAdapter)
7369
{
70+
logPath = logPath ?? "DebugAdapter.log";
7471
server = new DebugAdapter();
7572
}
7673
else
7774
{
75+
logPath = logPath ?? "EditorServices.log";
7876
server = new LanguageServer();
7977
}
8078

79+
// Start the logger with the specified log path
80+
// TODO: Set the level based on command line parameter
81+
Logger.Initialize(logPath, LogLevel.Verbose);
82+
83+
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host starting...");
84+
85+
// Start the server
8186
server.Start();
87+
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host started!");
88+
89+
// Wait for the server to finish
8290
server.WaitForExit();
8391

8492
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host exited normally.");

src/PowerShellEditorServices/Utility/Logger.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ private bool TryOpenLogFile(
185185
{
186186
try
187187
{
188+
// Make sure the log directory exists
189+
Directory.CreateDirectory(
190+
Path.GetDirectoryName(
191+
logFilePath));
192+
188193
// Open the log file for writing with UTF8 encoding
189194
this.textWriter =
190195
new StreamWriter(

test/PowerShellEditorServices.Test.Host/DebugAdapterTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
using Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
99
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
10+
using System;
1011
using System.IO;
1112
using System.Threading.Tasks;
1213
using Xunit;
14+
using Xunit.Abstractions;
15+
using Xunit.Sdk;
1316

1417
namespace Microsoft.PowerShell.EditorServices.Test.Host
1518
{
@@ -21,11 +24,21 @@ public class DebugAdapterTests : IAsyncLifetime
2124

2225
public Task InitializeAsync()
2326
{
27+
string testLogPath =
28+
Path.Combine(
29+
AppDomain.CurrentDomain.BaseDirectory,
30+
"logs",
31+
this.GetType().Name,
32+
Guid.NewGuid().ToString().Substring(0, 8) + ".log");
33+
34+
Console.WriteLine(" Output log at path: {0}", testLogPath);
35+
2436
this.debugAdapterClient =
2537
new DebugAdapterClient(
2638
new StdioClientChannel(
2739
"Microsoft.PowerShell.EditorServices.Host.exe",
28-
"/debugAdapter"));
40+
"/debugAdapter",
41+
"/logPath:\"" + testLogPath + "\""));
2942

3043
return this.debugAdapterClient.Start();
3144
}

test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Linq;
1414
using System.Threading.Tasks;
1515
using Xunit;
16+
using Xunit.Abstractions;
1617

1718
namespace Microsoft.PowerShell.EditorServices.Test.Host
1819
{
@@ -22,10 +23,20 @@ public class LanguageServerTests : IAsyncLifetime
2223

2324
public Task InitializeAsync()
2425
{
26+
string testLogPath =
27+
Path.Combine(
28+
AppDomain.CurrentDomain.BaseDirectory,
29+
"logs",
30+
this.GetType().Name,
31+
Guid.NewGuid().ToString().Substring(0, 8) + ".log");
32+
33+
Console.WriteLine(" Output log at path: {0}", testLogPath);
34+
2535
this.languageServiceClient =
2636
new LanguageServiceClient(
2737
new StdioClientChannel(
28-
"Microsoft.PowerShell.EditorServices.Host.exe"));
38+
"Microsoft.PowerShell.EditorServices.Host.exe",
39+
"/logPath:\"" + testLogPath + "\""));
2940

3041
return this.languageServiceClient.Start();
3142
}

0 commit comments

Comments
 (0)