Path: redb-tsak/src/redb.Tsak.Web/Components/Pages/Logs.razor Snapshot: redb-tsak, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-tsak Page: https://redbase.app/src/dev-2026-12-06/redb-tsak/src/redb.Tsak.Web/Components/Pages/Logs.razor ---- @page "/node/{NodeId}/logs" @* Operator tier: matches the worker API (LogsController — log bodies may carry payload fragments; review 2026-09-02 В13). *@ @attribute [Authorize(Policy = "Operator")] @inject INodeClientProvider NodeProvider @inject ILogger Log @inject ToastService Toast @inject NavigationManager Nav @inject IJSRuntime JS @inject LogTailCache Tails @using redb.Tsak.Client @using redb.Tsak.Contracts @using Microsoft.AspNetCore.Components.Web.Virtualization @implements IDisposable
@if (_activeTab == "live") { @* Filters *@
@(_tail?.LineCount ?? 0) shown / @(_tail?.BufferCapacity ?? 0) in the node's buffer
@if (_tail is { Unavailable: true }) {
This node keeps no log buffer.
} @* Log entries: the rows the page holds, filtered here; only the ones on screen are rendered. *@
@if (_tail is { HasOlder: true }) {
} @{ var view = FilteredRows; } @if (view.Count == 0) {

No log entries

} else { @if (row.Entry is { } entry) { } else { }
@LogTime.Header Level Message Source
@LogTime.Format(entry.Timestamp) @entry.Message @if (!string.IsNullOrEmpty(entry.Exception)) {
@entry.Exception
}
@(ShortenSource(entry.Source))
@row.Marker
}
} else { @* Files tab *@ @if (_filesLoading) {

Loading log files…

} else if (_logFiles is { Length: > 0 }) {
Log Files
Filename Size Last Modified (UTC) Download @context.Name @FormatSize(context.Size) @context.LastModified.ToString("yyyy-MM-dd HH:mm:ss") ZIP
} else {

No log files available on this node

} }
@code { [Parameter] public string NodeId { get; set; } = ""; private string _activeTab = "live"; private string _levelFilter = ""; private string _searchText = ""; private bool _autoScroll = true; // The tab, the level and the search travel in the address (?tab=files, ?level=ERR, ?q=..), so a reload, a shared // link and the node switcher keep them. The search is written when the field is left, not on every key. private static readonly string[] Tabs = ["live", "files"]; private static readonly string[] Levels = ["", "VRB", "DBG", "INF", "WRN", "ERR", "FTL"]; [SupplyParameterFromQuery(Name = "tab")] public string? TabQuery { get; set; } [SupplyParameterFromQuery(Name = "level")] public string? LevelQuery { get; set; } [SupplyParameterFromQuery(Name = "q")] public string? SearchQuery { get; set; } private void WriteFilters() => QueryState.Set(Nav, new Dictionary { ["level"] = _levelFilter, ["q"] = _searchText }); private void SwitchToLive() { _activeTab = "live"; QueryState.Set(Nav, "tab", _activeTab, "live"); } // Live state: the node's tail, kept by the circuit (LogTailCache), so a return to this page shows what was seen and // reads only what is new. private LogTail? _tail; private bool _shouldScroll; private bool _loadingOlder; private bool _jumpToBottom; // Files state private LogFileInfo[]? _logFiles; private bool _filesLoading; private PeriodicTimer? _timer; private CancellationTokenSource? _cts; // Component-scoped token: API calls abort when the component is disposed (navigated away) (4.9). // Guard the disposed state — reading _cts.Token after Dispose throws ObjectDisposedException; a call // that started before Dispose already holds the (now-cancelled) token, so returning None here is safe. private CancellationToken Ct { get { try { return _cts?.Token ?? default; } catch (ObjectDisposedException) { return default; } } } // The rows the page holds, filtered here (no request to the node); markers always stay. private List FilteredRows => LogFilter.Apply(_tail?.Rows ?? [], _levelFilter, _searchText); protected override async Task OnInitializedAsync() { // _cts BEFORE the initial load — a component disposed mid-load must not start an // unstoppable orphan poll loop afterwards (review 2026-09-02, С19). _cts = new CancellationTokenSource(); _activeTab = QueryState.OneOf(TabQuery, Tabs, "live"); _levelFilter = QueryState.OneOf(LevelQuery, Levels, ""); _searchText = SearchQuery ?? ""; _tail = Tails.Get(NodeId); if (_activeTab == "files") await LoadFiles(); await FetchIncremental(poll: false); // Opening the page shows the newest lines, wherever the page was scrolled last time. _jumpToBottom = true; if (!_cts.IsCancellationRequested) _ = PollLoop(_cts.Token); } protected override async Task OnAfterRenderAsync(bool firstRender) { if (_jumpToBottom) { _jumpToBottom = false; _shouldScroll = false; await JS.InvokeVoidAsync("tsakLogs.scrollToBottom", "logScrollContainer"); } else if (_shouldScroll) { _shouldScroll = false; await JS.InvokeVoidAsync("tsakLogs.autoScroll", "logScrollContainer"); } } private Task PollLoop(CancellationToken ct) { _timer = new PeriodicTimer(TimeSpan.FromSeconds(3)); return PollLoops.RunAsync(_timer, ct, Log, async () => { if (_activeTab == "live") { await FetchIncremental(poll: true); await InvokeAsync(StateHasChanged); } }); } private async Task FetchIncremental(bool poll) { if (_tail is null || NodeProvider.GetClient(NodeId) is not { } client) return; // The node's start time from the topology tells a restart apart; a poll reads the cached topology only. var topology = poll ? NodeProvider.Topology : NodeProvider.Topology ?? await NodeProvider.LoadTopologyAsync(); var started = topology?.Nodes.FirstOrDefault(n => n.NodeId == NodeId)?.StartedAt; var startedAt = started is { } s && s != default ? s : (DateTimeOffset?)null; var before = _tail.LastId; try { await _tail.SyncAsync(new ClientLogSource(client), startedAt, poll, Ct); } catch (OperationCanceledException) when (Ct.IsCancellationRequested) { } catch (Exception ex) { // A node that does not answer leaves the lines on screen; the next poll tries again. Log.LogDebug(ex, "Reading the log of node {Node} failed", NodeId); } if (_tail.LastId != before) _shouldScroll = _autoScroll; } private async Task LoadOlder() { if (_tail is null || NodeProvider.GetClient(NodeId) is not { } client) return; _loadingOlder = true; try { await _tail.LoadOlderAsync(new ClientLogSource(client), Ct); } catch (OperationCanceledException) when (Ct.IsCancellationRequested) { } catch (Exception ex) { Toast.Error($"Could not load older lines: {ex.Message}"); } finally { _loadingOlder = false; } } private async Task RefreshLive() { if (_activeTab == "live") await FetchIncremental(poll: false); else await LoadFiles(); } // Clears the view; new lines keep coming, and "load older" brings the cleared ones back. private void ClearLive() => _tail?.Clear(); private async Task SwitchToFiles() { _activeTab = "files"; QueryState.Set(Nav, "tab", _activeTab, "live"); await LoadFiles(); } private async Task LoadFiles() { var client = NodeProvider.GetClient(NodeId); if (client is null) return; _filesLoading = true; try { var response = await client.GetLogFilesAsync(Ct); _logFiles = response is { Available: true } ? response.Files : []; } catch { _logFiles = []; } _filesLoading = false; } private static string GetLevelVariant(string level) => level switch { "VRB" => "muted", "DBG" => "muted", "INF" => "online", "WRN" => "warning", "ERR" => "error", "FTL" => "error", _ => "muted" }; private static string GetRowStyle(string level) => level switch { "WRN" => "background: rgba(var(--warning-rgb, 255,171,0), 0.06);", "ERR" or "FTL" => "background: rgba(var(--error-rgb, 255,68,68), 0.08);", _ => "" }; private static string ShortenSource(string? source) { if (string.IsNullOrEmpty(source)) return "—"; // Show last two segments: "redb.Tsak.Core.Services.TsakCoordinator" → "Services.TsakCoordinator" var parts = source.Split('.'); return parts.Length > 2 ? string.Join('.', parts[^2..]) : source; } private static string FormatSize(long bytes) { if (bytes < 1024) return $"{bytes} B"; if (bytes < 1024 * 1024) return $"{bytes / 1024.0:F1} KB"; return $"{bytes / (1024.0 * 1024.0):F1} MB"; } public void Dispose() { _cts?.Cancel(); _cts?.Dispose(); _timer?.Dispose(); } }