Skip to content

Commit f2ba446

Browse files
authored
Add Error Logging for the Blazor DebugProxyLauncher (#43721)
* Add Error Logging for the Blazor DebugProxyLauncher * Fixes #42614 * Update DebugProxyLauncher.cs * typo fix * Update src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs
1 parent ddfbb07 commit f2ba446

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ internal static class DebugProxyLauncher
2020
private static readonly Regex ApplicationStartedRegex = new Regex(@"^\s*Application started\. Press Ctrl\+C to shut down\.$", RegexOptions.None, TimeSpan.FromSeconds(10));
2121
private static readonly string[] MessageSuppressionPrefixes = new[]
2222
{
23-
"Hosting environment:",
24-
"Content root path:",
25-
"Now listening on:",
26-
"Application started. Press Ctrl+C to shut down.",
27-
};
23+
"Hosting environment:",
24+
"Content root path:",
25+
"Now listening on:",
26+
"Application started. Press Ctrl+C to shut down.",
27+
};
2828

2929
public static Task<string> EnsureLaunchedAndGetUrl(IServiceProvider serviceProvider, string devToolsHost)
3030
{
3131
lock (LaunchLock)
3232
{
33-
if (LaunchedDebugProxyUrl == null)
34-
{
35-
LaunchedDebugProxyUrl = LaunchAndGetUrl(serviceProvider, devToolsHost);
36-
}
33+
LaunchedDebugProxyUrl ??= LaunchAndGetUrl(serviceProvider, devToolsHost);
3734

3835
return LaunchedDebugProxyUrl;
3936
}
@@ -54,6 +51,7 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
5451
Arguments = $"exec \"{executablePath}\" --OwnerPid {ownerPid} --DevToolsUrl {devToolsHost}",
5552
UseShellExecute = false,
5653
RedirectStandardOutput = true,
54+
RedirectStandardError = true,
5755
};
5856
RemoveUnwantedEnvironmentVariables(processStartInfo.Environment);
5957

@@ -141,21 +139,40 @@ private static void PassThroughConsoleOutput(Process process)
141139
private static void CompleteTaskWhenServerIsReady(Process aspNetProcess, TaskCompletionSource<string> taskCompletionSource)
142140
{
143141
string? capturedUrl = null;
142+
var errorEncountered = false;
143+
144+
aspNetProcess.ErrorDataReceived += OnErrorDataReceived;
145+
aspNetProcess.BeginErrorReadLine();
146+
144147
aspNetProcess.OutputDataReceived += OnOutputDataReceived;
145148
aspNetProcess.BeginOutputReadLine();
146149

150+
void OnErrorDataReceived(object sender, DataReceivedEventArgs eventArgs)
151+
{
152+
if (!string.IsNullOrEmpty(eventArgs.Data))
153+
{
154+
taskCompletionSource.TrySetException(new InvalidOperationException(
155+
eventArgs.Data));
156+
errorEncountered = true;
157+
}
158+
}
159+
147160
void OnOutputDataReceived(object sender, DataReceivedEventArgs eventArgs)
148161
{
149162
if (string.IsNullOrEmpty(eventArgs.Data))
150163
{
151-
taskCompletionSource.TrySetException(new InvalidOperationException(
152-
"No output has been recevied from the application."));
164+
if (!errorEncountered)
165+
{
166+
taskCompletionSource.TrySetException(new InvalidOperationException(
167+
"Expected output has not been received from the application."));
168+
}
153169
return;
154170
}
155171

156172
if (ApplicationStartedRegex.IsMatch(eventArgs.Data))
157173
{
158174
aspNetProcess.OutputDataReceived -= OnOutputDataReceived;
175+
aspNetProcess.ErrorDataReceived -= OnErrorDataReceived;
159176
if (!string.IsNullOrEmpty(capturedUrl))
160177
{
161178
taskCompletionSource.TrySetResult(capturedUrl);

0 commit comments

Comments
 (0)