Skip to content

Commit f27ec97

Browse files
authored
Better client / server logging (dotnet#46079)
1 parent 764803c commit f27ec97

File tree

7 files changed

+41
-26
lines changed

7 files changed

+41
-26
lines changed

Diff for: Compilers.sln

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpErrorFactsGenerator",
6262
EndProject
6363
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpSyntaxGenerator", "src\Tools\Source\CompilerGeneratorTools\Source\CSharpSyntaxGenerator\CSharpSyntaxGenerator.csproj", "{288089C5-8721-458E-BE3E-78990DAB5E2D}"
6464
EndProject
65-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IOperationGenerator", "src\Tools\Source\CompilerGeneratorTools\Source\IOperationGenerator\CompilersIOperationGenerator.csproj", "{D0A79850-B32A-45E5-9FD5-D43CB345867A}"
65+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompilersIOperationGenerator", "src\Tools\Source\CompilerGeneratorTools\Source\IOperationGenerator\CompilersIOperationGenerator.csproj", "{D0A79850-B32A-45E5-9FD5-D43CB345867A}"
6666
EndProject
6767
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VisualBasicSyntaxGenerator", "src\Tools\Source\CompilerGeneratorTools\Source\VisualBasicSyntaxGenerator\VisualBasicSyntaxGenerator.vbproj", "{6AA96934-D6B7-4CC8-990D-DB6B9DD56E34}"
6868
EndProject
@@ -303,10 +303,10 @@ Global
303303
{288089C5-8721-458E-BE3E-78990DAB5E2D}.Debug|Any CPU.Build.0 = Debug|x64
304304
{288089C5-8721-458E-BE3E-78990DAB5E2D}.Release|Any CPU.ActiveCfg = Release|x64
305305
{288089C5-8721-458E-BE3E-78990DAB5E2D}.Release|Any CPU.Build.0 = Release|x64
306-
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Debug|Any CPU.ActiveCfg = Debug|x64
307-
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Debug|Any CPU.Build.0 = Debug|x64
308-
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Release|Any CPU.ActiveCfg = Release|x64
309-
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Release|Any CPU.Build.0 = Release|x64
306+
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
307+
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Debug|Any CPU.Build.0 = Debug|Any CPU
308+
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Release|Any CPU.ActiveCfg = Release|Any CPU
309+
{D0A79850-B32A-45E5-9FD5-D43CB345867A}.Release|Any CPU.Build.0 = Release|Any CPU
310310
{6AA96934-D6B7-4CC8-990D-DB6B9DD56E34}.Debug|Any CPU.ActiveCfg = Debug|x64
311311
{6AA96934-D6B7-4CC8-990D-DB6B9DD56E34}.Debug|Any CPU.Build.0 = Debug|x64
312312
{6AA96934-D6B7-4CC8-990D-DB6B9DD56E34}.Release|Any CPU.ActiveCfg = Release|x64

Diff for: src/Compilers/Core/CommandLine/CompilerServerLogger.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void LogException(Exception exception, string reason)
7979
{
8080
if (s_loggingStream != null)
8181
{
82-
Log("Exception '{0}' occurred during '{1}'. Stack trace:\r\n{2}", exception.Message, reason, exception.StackTrace);
82+
LogError("Exception '{0}' occurred during '{1}'. Stack trace:\r\n{2}", exception.Message, reason, exception.StackTrace);
8383

8484
int innerExceptionLevel = 0;
8585

@@ -125,6 +125,10 @@ public static void Log(string message)
125125
}
126126
}
127127

128+
public static void LogError(string message) => Log($"Error: {message}");
129+
130+
public static void LogError(string format, params object?[] arguments) => Log($"Error: {format}", arguments);
131+
128132
private static int GetCurrentProcessId()
129133
{
130134
var process = Process.GetCurrentProcess();

Diff for: src/Compilers/Core/MSBuildTask/CopyRefAssembly.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ private bool Copy()
8888
}
8989
catch (Exception e)
9090
{
91-
Log.LogErrorWithCodeFromResources("Compiler_UnexpectedException");
92-
ManagedCompiler.LogErrorOutput(e.ToString(), Log);
91+
var util = new TaskLoggingHelper(this);
92+
util.LogErrorWithCodeFromResources("Compiler_UnexpectedException");
93+
util.LogErrorFromException(e, showStackTrace: true, showDetail: true, file: null);
9394
return false;
9495
}
9596

Diff for: src/Compilers/Core/MSBuildTask/ManagedCompiler.cs

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Build.Utilities;
1717
using Roslyn.Utilities;
1818
using Microsoft.CodeAnalysis.CommandLine;
19+
using Microsoft.Build.Tasks;
1920

2021
namespace Microsoft.CodeAnalysis.BuildTasks
2122
{
@@ -526,6 +527,7 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand
526527
}
527528
else
528529
{
530+
CompilerServerLogger.LogError($"Server compilation failed, falling back to {pathToTool}");
529531
Log.LogMessage(ErrorString.SharedCompilationFallback, pathToTool);
530532

531533
ExitCode = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
@@ -538,8 +540,9 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand
538540
}
539541
catch (Exception e)
540542
{
541-
Log.LogErrorWithCodeFromResources("Compiler_UnexpectedException");
542-
LogErrorOutput(e.ToString());
543+
var util = new TaskLoggingHelper(this);
544+
util.LogErrorWithCodeFromResources("Compiler_UnexpectedException");
545+
util.LogErrorFromException(e, showStackTrace: true, showDetail: true, file: null);
543546
ExitCode = -1;
544547
}
545548

@@ -623,7 +626,7 @@ private int HandleResponse(BuildResponse response, string pathToTool, string res
623626

624627
if (LogStandardErrorAsError)
625628
{
626-
LogErrorOutput(completedResponse.ErrorOutput);
629+
LogErrorMultiline(completedResponse.ErrorOutput);
627630
}
628631
else
629632
{
@@ -633,37 +636,39 @@ private int HandleResponse(BuildResponse response, string pathToTool, string res
633636
return completedResponse.ReturnCode;
634637

635638
case BuildResponse.ResponseType.MismatchedVersion:
636-
LogErrorOutput("Roslyn compiler server reports different protocol version than build task.");
639+
LogError("Roslyn compiler server reports different protocol version than build task.");
637640
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
638641

639642
case BuildResponse.ResponseType.IncorrectHash:
640-
LogErrorOutput("Roslyn compiler server reports different hash version than build task.");
643+
LogError("Roslyn compiler server reports different hash version than build task.");
641644
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
642645

643646
case BuildResponse.ResponseType.Rejected:
644647
case BuildResponse.ResponseType.AnalyzerInconsistency:
648+
CompilerServerLogger.LogError($"Server rejected request {response.Type}");
645649
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
646650

647651
default:
648-
LogErrorOutput($"Received an unrecognized response from the server: {response.Type}");
652+
LogError($"Received an unrecognized response from the server: {response.Type}");
649653
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
650654
}
651655
}
652656

653-
private void LogErrorOutput(string output)
657+
internal void LogError(string message)
654658
{
655-
LogErrorOutput(output, Log);
659+
CompilerServerLogger.LogError(message);
660+
Log.LogError(message);
656661
}
657662

658-
internal static void LogErrorOutput(string output, TaskLoggingHelper log)
663+
private void LogErrorMultiline(string output)
659664
{
660665
string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
661666
foreach (string line in lines)
662667
{
663668
string trimmedMessage = line.Trim();
664669
if (trimmedMessage != "")
665670
{
666-
log.LogError(trimmedMessage);
671+
Log.LogError(trimmedMessage);
667672
}
668673
}
669674
}

Diff for: src/Compilers/Server/VBCSCompiler/AnalyzerConsistencyChecker.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static bool CheckCore(string baseDirectory, IEnumerable<CommandLineAnaly
6868
{
6969
if (!ignorableReferenceNames.Any(name => missingDependency.Name.StartsWith(name)))
7070
{
71-
CompilerServerLogger.Log($"Analyzer assembly {resolvedPath} depends on '{missingDependency}' but it was not found.");
71+
CompilerServerLogger.LogError($"Analyzer assembly {resolvedPath} depends on '{missingDependency}' but it was not found.");
7272
return false;
7373
}
7474
}
@@ -98,7 +98,7 @@ private static bool CheckCore(string baseDirectory, IEnumerable<CommandLineAnaly
9898

9999
if (resolvedPathMvid != loadedAssemblyMvid)
100100
{
101-
CompilerServerLogger.Log($"Analyzer assembly {resolvedPath} has MVID '{resolvedPathMvid}' but loaded assembly '{loadedAssembly.FullName}' has MVID '{loadedAssemblyMvid}'.");
101+
CompilerServerLogger.LogError($"Analyzer assembly {resolvedPath} has MVID '{resolvedPathMvid}' but loaded assembly '{loadedAssembly.FullName}' has MVID '{loadedAssemblyMvid}'.");
102102
return false;
103103
}
104104
}

Diff for: src/Compilers/Server/VBCSCompiler/ServerDispatcher.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,21 @@ private void HandleCompletedConnections()
294294
{
295295
case CompletionReason.CompilationCompleted:
296296
case CompletionReason.CompilationNotStarted:
297+
CompilerServerLogger.Log("Client completed");
297298
// These are all normal shutdown states. Nothing to do here.
298299
break;
299300
case CompletionReason.ClientDisconnect:
300301
// Have to assume the worst here which is user pressing Ctrl+C at the command line and
301302
// hence wanting all compilation to end.
302-
CompilerServerLogger.Log("Unexpected client disconnect. Shutting down server");
303+
CompilerServerLogger.LogError("Unexpected client disconnect. Shutting down server");
303304
shutdown = true;
304305
break;
305306
case CompletionReason.ClientException:
307+
CompilerServerLogger.LogError($"Unexpected client exception. Shutting down server");
308+
shutdown = true;
309+
break;
306310
case CompletionReason.ClientShutdownRequest:
307-
CompilerServerLogger.Log($"Unexpected client completion: {connectionData.CompletionReason}. Shutting down server");
311+
CompilerServerLogger.Log($"Client requesting server shutdown");
308312
shutdown = true;
309313
break;
310314
default:
@@ -316,6 +320,7 @@ private void HandleCompletedConnections()
316320

317321
if (shutdown)
318322
{
323+
CompilerServerLogger.Log($"Shutting down server");
319324
_state = State.ShuttingDown;
320325
}
321326
}

Diff for: src/Compilers/Shared/BuildServerConnection.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private static async Task<BuildResponse> TryCompileAsync(NamedPipeClientStream p
288288
}
289289
else
290290
{
291-
Log("Client disconnect");
291+
LogError("Client disconnect");
292292
response = new RejectedBuildResponse($"Client disconnected");
293293
}
294294

@@ -382,7 +382,7 @@ internal static async Task MonitorDisconnectAsync(
382382
// IOException: The server is connected to another client and the
383383
// time-out period has expired.
384384

385-
Log($"Connecting to server timed out after {timeoutMs} ms");
385+
LogException(e, $"Connecting to server timed out after {timeoutMs} ms");
386386
return null;
387387
}
388388
Log("Named pipe '{0}' connected", pipeName);
@@ -392,7 +392,7 @@ internal static async Task MonitorDisconnectAsync(
392392
// Verify that we own the pipe.
393393
if (!NamedPipeUtil.CheckPipeConnectionOwnership(pipeStream))
394394
{
395-
Log("Owner of named pipe is incorrect");
395+
LogError("Owner of named pipe is incorrect");
396396
return null;
397397
}
398398

@@ -461,7 +461,7 @@ internal static bool TryCreateServerCore(string clientDir, string pipeName)
461461
}
462462
else
463463
{
464-
Log("Failed to create process. GetLastError={0}", Marshal.GetLastWin32Error());
464+
LogError("Failed to create process. GetLastError={0}", Marshal.GetLastWin32Error());
465465
}
466466
return success;
467467
}

0 commit comments

Comments
 (0)