Skip to content

Commit 9e69242

Browse files
authored
Add logging to RemoteRenderer to time a round-trip (#12638)
1 parent aab75e8 commit 9e69242

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<Compile Include="Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs" />
88
<Reference Include="Microsoft.AspNetCore.Components.Web" />
99
<Reference Include="Microsoft.AspNetCore.DataProtection" />
10-
<Reference Include="Microsoft.Extensions.Logging" />
1110
<Reference Include="Microsoft.AspNetCore.SignalR" />
1211
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
1312
<Reference Include="Microsoft.Extensions.Caching.Memory" />
1413
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
1514
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
15+
<Reference Include="Microsoft.Extensions.Logging" />
1616
</ItemGroup>
1717
</Project>

src/Components/Server/src/Circuits/RemoteRenderer.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Text.Encodings.Web;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Components.Rendering;
1112
using Microsoft.AspNetCore.Components.Server.Circuits;
1213
using Microsoft.AspNetCore.SignalR;
14+
using Microsoft.Extensions.Internal;
1315
using Microsoft.Extensions.Logging;
1416
using Microsoft.JSInterop;
1517

@@ -138,7 +140,8 @@ protected override Task UpdateDisplayAsync(in RenderBatch batch)
138140
pendingRender = new UnacknowledgedRenderBatch(
139141
renderId,
140142
arrayBuilder,
141-
new TaskCompletionSource<object>());
143+
new TaskCompletionSource<object>(),
144+
ValueStopwatch.StartNew());
142145

143146
// Buffer the rendered batches no matter what. We'll send it down immediately when the client
144147
// is connected or right after the client reconnects.
@@ -256,13 +259,14 @@ public void OnRenderCompleted(long incomingBatchId, string errorMessageOrNull)
256259

257260
private void ProcessPendingBatch(string errorMessageOrNull, UnacknowledgedRenderBatch entry)
258261
{
262+
var elapsedTime = entry.ValueStopwatch.GetElapsedTime();
259263
if (errorMessageOrNull == null)
260264
{
261-
Log.CompletingBatchWithoutError(_logger, entry.BatchId);
265+
Log.CompletingBatchWithoutError(_logger, entry.BatchId, elapsedTime);
262266
}
263267
else
264268
{
265-
Log.CompletingBatchWithError(_logger, entry.BatchId, errorMessageOrNull);
269+
Log.CompletingBatchWithError(_logger, entry.BatchId, errorMessageOrNull, elapsedTime);
266270
}
267271

268272
entry.Data.Dispose();
@@ -283,16 +287,18 @@ private void CompleteRender(TaskCompletionSource<object> pendingRenderInfo, stri
283287

284288
internal readonly struct UnacknowledgedRenderBatch
285289
{
286-
public UnacknowledgedRenderBatch(long batchId, ArrayBuilder<byte> data, TaskCompletionSource<object> completionSource)
290+
public UnacknowledgedRenderBatch(long batchId, ArrayBuilder<byte> data, TaskCompletionSource<object> completionSource, ValueStopwatch valueStopwatch)
287291
{
288292
BatchId = batchId;
289293
Data = data;
290294
CompletionSource = completionSource;
295+
ValueStopwatch = valueStopwatch;
291296
}
292297

293298
public long BatchId { get; }
294299
public ArrayBuilder<byte> Data { get; }
295300
public TaskCompletionSource<object> CompletionSource { get; }
301+
public ValueStopwatch ValueStopwatch { get; }
296302
}
297303

298304
private void CaptureAsyncExceptions(Task task)
@@ -312,8 +318,8 @@ private static class Log
312318
private static readonly Action<ILogger, long, int, string, Exception> _beginUpdateDisplayAsync;
313319
private static readonly Action<ILogger, string, Exception> _bufferingRenderDisconnectedClient;
314320
private static readonly Action<ILogger, string, Exception> _sendBatchDataFailed;
315-
private static readonly Action<ILogger, long, string, Exception> _completingBatchWithError;
316-
private static readonly Action<ILogger, long, Exception> _completingBatchWithoutError;
321+
private static readonly Action<ILogger, long, string, double, Exception> _completingBatchWithError;
322+
private static readonly Action<ILogger, long, double, Exception> _completingBatchWithoutError;
317323
private static readonly Action<ILogger, long, Exception> _receivedDuplicateBatchAcknowledgement;
318324

319325
private static class EventIds
@@ -349,15 +355,15 @@ static Log()
349355
EventIds.SendBatchDataFailed,
350356
"Sending data for batch failed: {Message}");
351357

352-
_completingBatchWithError = LoggerMessage.Define<long, string>(
358+
_completingBatchWithError = LoggerMessage.Define<long, string, double>(
353359
LogLevel.Debug,
354360
EventIds.CompletingBatchWithError,
355-
"Completing batch {BatchId} with error: {ErrorMessage}");
361+
"Completing batch {BatchId} with error: {ErrorMessage} in {ElapsedMilliseconds}ms.");
356362

357-
_completingBatchWithoutError = LoggerMessage.Define<long>(
363+
_completingBatchWithoutError = LoggerMessage.Define<long, double>(
358364
LogLevel.Debug,
359365
EventIds.CompletingBatchWithoutError,
360-
"Completing batch {BatchId} without error");
366+
"Completing batch {BatchId} without error in {ElapsedMilliseconds}ms.");
361367

362368
_receivedDuplicateBatchAcknowledgement = LoggerMessage.Define<long>(
363369
LogLevel.Debug,
@@ -396,20 +402,22 @@ public static void BufferingRenderDisconnectedClient(ILogger logger, string conn
396402
null);
397403
}
398404

399-
public static void CompletingBatchWithError(ILogger logger, long batchId, string errorMessage)
405+
public static void CompletingBatchWithError(ILogger logger, long batchId, string errorMessage, TimeSpan elapsedTime)
400406
{
401407
_completingBatchWithError(
402408
logger,
403409
batchId,
404410
errorMessage,
411+
elapsedTime.TotalMilliseconds,
405412
null);
406413
}
407414

408-
public static void CompletingBatchWithoutError(ILogger logger, long batchId)
415+
public static void CompletingBatchWithoutError(ILogger logger, long batchId, TimeSpan elapsedTime)
409416
{
410417
_completingBatchWithoutError(
411418
logger,
412419
batchId,
420+
elapsedTime.TotalMilliseconds,
413421
null);
414422
}
415423

src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
@@ -14,12 +14,13 @@
1414
<ItemGroup>
1515
<Reference Include="Microsoft.AspNetCore.Components.Web" />
1616
<Reference Include="Microsoft.AspNetCore.DataProtection" />
17-
<Reference Include="Microsoft.Extensions.Logging" />
1817
<Reference Include="Microsoft.AspNetCore.SignalR" />
1918
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
2019
<Reference Include="Microsoft.Extensions.Caching.Memory" />
2120
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
2221
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
22+
<Reference Include="Microsoft.Extensions.Logging" />
23+
<Reference Include="Microsoft.Extensions.ValueStopwatch.Sources" PrivateAssets="All" />
2324
</ItemGroup>
2425

2526
<PropertyGroup>

0 commit comments

Comments
 (0)