Skip to content

Commit 7035364

Browse files
author
Henrik Zetterström
committed
Made ReadUserSessionLogs stream logs with IAsyncEnumerable and added cancelationtoken
1 parent efd5d7e commit 7035364

File tree

2 files changed

+23
-7
lines changed
  • src/Templates/Boilerplate/Bit.Boilerplate/src

2 files changed

+23
-7
lines changed

src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Pages/Management/UsersPage.razor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,12 @@ private void SearchSessions()
191191
/// </summary>
192192
private async Task ReadUserSessionLogs(Guid userSessionId)
193193
{
194-
var logs = await hubConnection.InvokeAsync<DiagnosticLogDto[]>("GetUserSessionLogs", userSessionId);
195-
196194
DiagnosticLogger.Store.Clear();
197-
foreach (var log in logs)
195+
196+
await foreach (var log in hubConnection.StreamAsync<DiagnosticLogDto>(
197+
"GetUserSessionLogs",
198+
userSessionId,
199+
cancellationToken: CurrentCancellationToken))
198200
{
199201
DiagnosticLogger.Store.Enqueue(log);
200202
}

src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/SignalR/AppHub.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//+:cnd:noEmit
2+
using System.Runtime.CompilerServices;
23
using Boilerplate.Server.Api.Controllers.Identity;
34
using Boilerplate.Server.Api.Models.Identity;
45
using Boilerplate.Shared.Dtos.Diagnostic;
@@ -66,17 +67,30 @@ public Task ChangeAuthenticationState(string? accessToken)
6667
/// <inheritdoc cref="SignalRMethods.UPLOAD_DIAGNOSTIC_LOGGER_STORE"/>
6768
/// </summary>
6869
[Authorize(Policy = AppFeatures.System.ManageLogs)]
69-
public async Task<DiagnosticLogDto[]> GetUserSessionLogs(Guid userSessionId, [FromServices] AppDbContext dbContext)
70+
public async IAsyncEnumerable<DiagnosticLogDto> GetUserSessionLogs(
71+
Guid userSessionId,
72+
[EnumeratorCancellation] CancellationToken cancellationToken,
73+
[FromServices] AppDbContext dbContext)
7074
{
7175
var userSessionSignalRConnectionId = await dbContext.UserSessions
7276
.Where(us => us.Id == userSessionId)
7377
.Select(us => us.SignalRConnectionId)
74-
.FirstOrDefaultAsync(Context.ConnectionAborted);
78+
.FirstOrDefaultAsync(cancellationToken);
7579

7680
if (string.IsNullOrEmpty(userSessionSignalRConnectionId))
77-
return [];
81+
yield break;
7882

79-
return await Clients.Client(userSessionSignalRConnectionId).InvokeAsync<DiagnosticLogDto[]>(SignalRMethods.UPLOAD_DIAGNOSTIC_LOGGER_STORE, Context.ConnectionAborted);
83+
var logs = await Clients.Client(userSessionSignalRConnectionId)
84+
.InvokeAsync<DiagnosticLogDto[]>(SignalRMethods.UPLOAD_DIAGNOSTIC_LOGGER_STORE, cancellationToken);
85+
86+
if (logs is null || logs.Length is 0)
87+
yield break;
88+
89+
foreach (var log in logs)
90+
{
91+
cancellationToken.ThrowIfCancellationRequested();
92+
yield return log;
93+
}
8094
}
8195

8296
private async Task ChangeAuthenticationStateImplementation(ClaimsPrincipal? user)

0 commit comments

Comments
 (0)