Skip to content

Commit

Permalink
Add log level select (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored and eerhardt committed Oct 27, 2023
1 parent 9c42302 commit be2cb34
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 21 deletions.
4 changes: 1 addition & 3 deletions src/Aspire.Dashboard/Components/Pages/Metrics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
<div class="metrics-layout">
<h1>Metrics</h1>
<FluentToolbar Orientation="Orientation.Horizontal">
<FluentSelect TOption="ApplicationViewModel"
Items="@_applications"
OptionValue="@(c => c.Id)"
<FluentSelect TOption="SelectViewModel<string>"
OptionText="@(c => c.Name)"
@bind-SelectedOption="_selectedApplication"
@bind-SelectedOption:after="HandleSelectedApplicationChangedAsync"
Expand Down
8 changes: 4 additions & 4 deletions src/Aspire.Dashboard/Components/Pages/Metrics.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Aspire.Dashboard.Components.Pages;

public partial class Metrics : IDisposable
{
private static readonly ApplicationViewModel s_selectApplication = new ApplicationViewModel { Id = null, Name = "Select service..." };
private static readonly SelectViewModel<string> s_selectApplication = new SelectViewModel<string> { Id = null, Name = "Select service..." };
private static readonly List<MetricsDurationViewModel> s_durations = new List<MetricsDurationViewModel>
{
new MetricsDurationViewModel { Text = "Last 1 minute", Duration = TimeSpan.FromMinutes(1) },
Expand All @@ -28,8 +28,8 @@ public partial class Metrics : IDisposable
};
private static readonly TimeSpan s_defaultDuration = TimeSpan.FromMinutes(5);

private List<ApplicationViewModel> _applications = default!;
private ApplicationViewModel _selectedApplication = s_selectApplication;
private List<SelectViewModel<string>> _applications = default!;
private SelectViewModel<string> _selectedApplication = s_selectApplication;
private MetricsDurationViewModel _selectedDuration = s_durations.Single(d => d.Duration == s_defaultDuration);
private Subscription? _applicationsSubscription;
private Subscription? _metricsSubscription;
Expand Down Expand Up @@ -105,7 +105,7 @@ protected override void OnParametersSet()

private void UpdateApplications()
{
_applications = TelemetryRepository.GetApplications().Select(a => new ApplicationViewModel { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications = TelemetryRepository.GetApplications().Select(a => new SelectViewModel<string> { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications.Insert(0, s_selectApplication);
UpdateSubscription();
}
Expand Down
16 changes: 13 additions & 3 deletions src/Aspire.Dashboard/Components/Pages/StructuredLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
<div class="logs-layout">
<h1>Structured Logs</h1>
<FluentToolbar Orientation="Orientation.Horizontal">
<FluentSelect TOption="ApplicationViewModel"
<FluentSelect TOption="SelectViewModel<string>"
Items="@_applications"
OptionValue="@(c => c.Id)"
OptionText="@(c => c.Name)"
@bind-SelectedOption="_selectedApplication"
@bind-SelectedOption:after="HandleSelectedApplicationChangedAsync" />
Expand All @@ -29,6 +28,17 @@
Placeholder="Filter..."
slot="end" />
<FluentDivider slot="end" Role="DividerRole.Presentation" Orientation="Orientation.Vertical" />
<span slot="end">Level:</span>
<FluentDivider slot="end" Role="DividerRole.Presentation" Orientation="Orientation.Vertical" />
<FluentSelect TOption="SelectViewModel<LogLevel>"
Items="@_logLevels"
OptionText="@(c => c.Name)"
@bind-SelectedOption="_selectedLogLevel"
@bind-SelectedOption:after="HandleSelectedLogLevelChangedAsync"
Width="120px"
Style="min-width: auto;"
slot="end" />
<FluentDivider slot="end" Role="DividerRole.Presentation" Orientation="Orientation.Vertical" />
<FluentLabel slot="end">Filters: </FluentLabel>
@if (ViewModel.Filters.Count == 0)
{
Expand All @@ -51,7 +61,7 @@
@context.Application.ApplicationName
</span>
</TemplateColumn>
<TemplateColumn Title="Severity">
<TemplateColumn Title="Level">
@if (context.Severity is LogLevel.Error or LogLevel.Critical)
{
<FluentIcon Icon="Icons.Filled.Size16.ErrorCircle" Color="Color.Error" Class="logs-severity-icon" />
Expand Down
32 changes: 28 additions & 4 deletions src/Aspire.Dashboard/Components/Pages/StructuredLogs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Otlp.Storage;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.Fast.Components.FluentUI;
using Microsoft.JSInterop;

namespace Aspire.Dashboard.Components.Pages;

public partial class StructuredLogs
{
private static readonly ApplicationViewModel s_allApplication = new ApplicationViewModel { Id = null, Name = "(All)" };
private static readonly SelectViewModel<string> s_allApplication = new SelectViewModel<string> { Id = null, Name = "(All)" };

private TotalItemsFooter _totalItemsFooter = default!;
private List<ApplicationViewModel> _applications = default!;
private ApplicationViewModel _selectedApplication = s_allApplication;
private List<SelectViewModel<string>> _applications = default!;
private List<SelectViewModel<LogLevel>> _logLevels = default!;
private SelectViewModel<string> _selectedApplication = s_allApplication;
private SelectViewModel<LogLevel> _selectedLogLevel = default!;
private Subscription? _applicationsSubscription;
private Subscription? _logsSubscription;
private bool _applicationChanged;
Expand Down Expand Up @@ -69,6 +73,18 @@ protected override Task OnInitializedAsync()
ViewModel.AddFilter(new LogFilter { Field = "SpanId", Condition = FilterCondition.Equals, Value = SpanId });
}

_logLevels = new List<SelectViewModel<LogLevel>>
{
new SelectViewModel<LogLevel> { Id = LogLevel.Trace, Name = "(All)" },
new SelectViewModel<LogLevel> { Id = LogLevel.Trace, Name = "Trace" },
new SelectViewModel<LogLevel> { Id = LogLevel.Debug, Name = "Debug" },
new SelectViewModel<LogLevel> { Id = LogLevel.Information, Name = "Information" },
new SelectViewModel<LogLevel> { Id = LogLevel.Warning, Name = "Warning" },
new SelectViewModel<LogLevel> { Id = LogLevel.Error, Name = "Error" },
new SelectViewModel<LogLevel> { Id = LogLevel.Critical, Name = "Critical" },
};
_selectedLogLevel = _logLevels[0];

UpdateApplications();
_applicationsSubscription = TelemetryRepository.OnNewApplications(() => InvokeAsync(() =>
{
Expand All @@ -88,7 +104,7 @@ protected override void OnParametersSet()

private void UpdateApplications()
{
_applications = TelemetryRepository.GetApplications().Select(a => new ApplicationViewModel { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications = TelemetryRepository.GetApplications().Select(a => new SelectViewModel<string> { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications.Insert(0, s_allApplication);
}

Expand All @@ -100,6 +116,14 @@ private Task HandleSelectedApplicationChangedAsync()
return Task.CompletedTask;
}

private Task HandleSelectedLogLevelChangedAsync()
{
ViewModel.LogLevel = _selectedLogLevel.Id;
_applicationChanged = true;

return Task.CompletedTask;
}

private void UpdateSubscription()
{
// Subscribe to updates.
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Dashboard/Components/Pages/Traces.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="traces-layout">
<h1>Traces</h1>
<FluentToolbar Orientation="Orientation.Horizontal">
<FluentSelect TOption="ApplicationViewModel"
<FluentSelect TOption="SelectViewModel<string>"
Items="@_applications"
OptionValue="@(c => c.Id)"
OptionText="@(c => c.Name)"
Expand Down
8 changes: 4 additions & 4 deletions src/Aspire.Dashboard/Components/Pages/Traces.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
namespace Aspire.Dashboard.Components.Pages;
public partial class Traces
{
private static readonly ApplicationViewModel s_allApplication = new ApplicationViewModel { Id = null, Name = "(All)" };
private static readonly SelectViewModel<string> s_allApplication = new SelectViewModel<string> { Id = null, Name = "(All)" };

private TotalItemsFooter _totalItemsFooter = default!;
private List<ApplicationViewModel> _applications = default!;
private ApplicationViewModel _selectedApplication = s_allApplication;
private List<SelectViewModel<string>> _applications = default!;
private SelectViewModel<string> _selectedApplication = s_allApplication;
private Subscription? _applicationsSubscription;
private Subscription? _tracesSubscription;
private bool _applicationChanged;
Expand Down Expand Up @@ -81,7 +81,7 @@ protected override void OnParametersSet()

private void UpdateApplications()
{
_applications = TelemetryRepository.GetApplications().Select(a => new ApplicationViewModel { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications = TelemetryRepository.GetApplications().Select(a => new SelectViewModel<string> { Id = a.InstanceId, Name = a.ApplicationName }).ToList();
_applications.Insert(0, s_allApplication);
UpdateSubscription();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Aspire.Dashboard.Model.Otlp;

public class ApplicationViewModel
public class SelectViewModel<T>
{
public required string Name { get; init; }
public required string? Id { get; init; }
public required T? Id { get; init; }
}
8 changes: 8 additions & 0 deletions src/Aspire.Dashboard/Model/StructuredLogsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Aspire.Dashboard.Model.Otlp;
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Otlp.Storage;
using Microsoft.Extensions.Logging;

namespace Aspire.Dashboard.Model;

Expand All @@ -17,6 +18,7 @@ public class StructuredLogsViewModel
private string _filterText = string.Empty;
private int _logsStartIndex;
private int? _logsCount;
private LogLevel? _logLevel;

public StructuredLogsViewModel(TelemetryRepository telemetryRepository)
{
Expand All @@ -42,6 +44,7 @@ public bool RemoveFilter(LogFilter filter)
}
public int StartIndex { get => _logsStartIndex; set => SetValue(ref _logsStartIndex, value); }
public int? Count { get => _logsCount; set => SetValue(ref _logsCount, value); }
public LogLevel? LogLevel { get => _logLevel; set => SetValue(ref _logLevel, value); }

private void SetValue<T>(ref T field, T value)
{
Expand All @@ -64,6 +67,11 @@ public PagedResult<OtlpLogEntry> GetLogs()
{
filters.Add(new LogFilter { Field = "Message", Condition = FilterCondition.Contains, Value = FilterText });
}
// If the log level is set and it is not the bottom level, which has no effect, then add a filter.
if (_logLevel != null && _logLevel != Microsoft.Extensions.Logging.LogLevel.Trace)
{
filters.Add(new LogFilter { Field = "Severity", Condition = FilterCondition.GreaterThanOrEqual, Value = _logLevel.Value.ToString() });
}

logs = _telemetryRepository.GetLogs(new GetLogsContext
{
Expand Down

0 comments on commit be2cb34

Please sign in to comment.