Skip to content

Commit

Permalink
Clear button on ConsoleLogs page
Browse files Browse the repository at this point in the history
  • Loading branch information
Daluur committed Feb 5, 2025
1 parent a49a22b commit 9ddc708
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
@bind-SelectedResource:after="HandleSelectedOptionChangedAsync"
LabelClass="toolbar-left" />

<ClearSignalsButton SelectedResource="PageViewModel.SelectedOption" HandleClearSignal="ClearConsoleLogs" />

@foreach (var command in _highlightedCommands)
{
<FluentButton Appearance="Appearance.Lightweight"
Expand Down
82 changes: 58 additions & 24 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Model.Otlp;
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Otlp.Storage;
using Aspire.Dashboard.Resources;
using Aspire.Dashboard.Utils;
using Aspire.Hosting.ConsoleLogs;
Expand Down Expand Up @@ -95,6 +96,9 @@ private sealed class ConsoleLogsSubscription
private bool _showTimestamp;
public ConsoleLogsViewModel PageViewModel { get; set; } = null!;

private readonly Dictionary<ApplicationKey, DateTime> _ignoreLogsBeforeTimeByApplicationKey = new Dictionary<ApplicationKey, DateTime>();
private DateTime _ignoreAllLogsBefore { get; set; } = DateTime.MinValue;

public string BasePath => DashboardUrls.ConsoleLogBasePath;
public string SessionStorageKey => BrowserStorageKeys.ConsoleLogsPageState;

Expand Down Expand Up @@ -217,35 +221,35 @@ protected override async Task OnParametersSetAsync()
Logger.LogDebug("New resource {ResourceName} selected.", selectedResourceName);

ConsoleLogsSubscription? newConsoleLogsSubscription = null;
if (selectedResourceName is not null)
{
newConsoleLogsSubscription = new ConsoleLogsSubscription { Name = selectedResourceName };
Logger.LogDebug("Creating new subscription {SubscriptionId}.", newConsoleLogsSubscription.SubscriptionId);
if (selectedResourceName is not null)
{
newConsoleLogsSubscription = new ConsoleLogsSubscription { Name = selectedResourceName };
Logger.LogDebug("Creating new subscription {SubscriptionId}.", newConsoleLogsSubscription.SubscriptionId);

if (Logger.IsEnabled(LogLevel.Debug))
if (Logger.IsEnabled(LogLevel.Debug))
{
newConsoleLogsSubscription.CancellationToken.Register(state =>
{
newConsoleLogsSubscription.CancellationToken.Register(state =>
{
var s = (ConsoleLogsSubscription)state!;
Logger.LogDebug("Canceling subscription {SubscriptionId} to {ResourceName}.", s.SubscriptionId, s.Name);
}, newConsoleLogsSubscription);
}
var s = (ConsoleLogsSubscription)state!;
Logger.LogDebug("Canceling subscription {SubscriptionId} to {ResourceName}.", s.SubscriptionId, s.Name);
}, newConsoleLogsSubscription);
}
}

if (_consoleLogsSubscription is { } currentSubscription)
{
currentSubscription.Cancel();
_consoleLogsSubscription = newConsoleLogsSubscription;
if (_consoleLogsSubscription is { } currentSubscription)
{
currentSubscription.Cancel();
_consoleLogsSubscription = newConsoleLogsSubscription;

await TaskHelpers.WaitIgnoreCancelAsync(currentSubscription.SubscriptionTask);
}
else
{
_consoleLogsSubscription = newConsoleLogsSubscription;
}
await TaskHelpers.WaitIgnoreCancelAsync(currentSubscription.SubscriptionTask);
}
else
{
_consoleLogsSubscription = newConsoleLogsSubscription;
}

Logger.LogDebug("Creating new log entries collection.");
_logEntries = new(Options.Value.Frontend.MaxConsoleLogCount);
Logger.LogDebug("Creating new log entries collection.");
_logEntries = new(Options.Value.Frontend.MaxConsoleLogCount);

if (newConsoleLogsSubscription is not null)
{
Expand Down Expand Up @@ -417,6 +421,17 @@ private void LoadLogs(ConsoleLogsSubscription newConsoleLogsSubscription)

try
{
var ignoreLogsBefore = _ignoreAllLogsBefore; // 'Global' ignore before timestamp, defaults to DateTime.Min
if (PageViewModel.SelectedOption.Id is not null &&
_ignoreLogsBeforeTimeByApplicationKey.TryGetValue(
PageViewModel.SelectedOption.Id.GetApplicationKey(),
out var _ignoreBeforeForApp) &&
_ignoreBeforeForApp > ignoreLogsBefore)
{
// If we have an entry for the specific app that is a higher value than the 'global' ignore, we use that.
ignoreLogsBefore = _ignoreBeforeForApp;
}

var logParser = new LogParser();
await foreach (var batch in subscription.ConfigureAwait(true))
{
Expand All @@ -434,7 +449,11 @@ private void LoadLogs(ConsoleLogsSubscription newConsoleLogsSubscription)
}

var logEntry = logParser.CreateLogEntry(content, isErrorOutput);
_logEntries.InsertSorted(logEntry);
if (logEntry.Timestamp is null || logEntry.Timestamp > ignoreLogsBefore)
{
// Only add entries that are not ignored, or if they are null as we cannot know when they happened.
_logEntries.InsertSorted(logEntry);
}
}

await InvokeAsync(StateHasChanged);
Expand Down Expand Up @@ -531,6 +550,21 @@ private async Task DownloadLogsAsync()
await JS.InvokeVoidAsync("downloadStreamAsFile", fileName, streamReference);
}

private async Task ClearConsoleLogs(ApplicationKey? key)
{
if (key is null)
{
_ignoreAllLogsBefore = DateTime.UtcNow;
}
else
{
_ignoreLogsBeforeTimeByApplicationKey[key.Value] = DateTime.UtcNow;
}

_logEntries.Clear();
await InvokeAsync(StateHasChanged);
}

public async ValueTask DisposeAsync()
{
_resourceSubscriptionCts.Cancel();
Expand Down

0 comments on commit 9ddc708

Please sign in to comment.