Skip to content

Truncate large messages displayed in log grid #10882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Aspire.Dashboard/Components/Pages/StructuredLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
<AspireTemplateColumn ColumnId="@TimestampColumn" ColumnManager="@_manager" Title="@Loc[nameof(Dashboard.Resources.StructuredLogs.StructuredLogsTimestampColumnHeader)]" TooltipText="@(context => FormatHelpers.FormatDateTime(TimeProvider, context.TimeStamp, MillisecondsDisplay.Full, CultureInfo.CurrentCulture))" Tooltip="true">
@FormatHelpers.FormatTimeWithOptionalDate(TimeProvider, context.TimeStamp, MillisecondsDisplay.Truncated)
</AspireTemplateColumn>
<AspireTemplateColumn ColumnId="@MessageColumn" ColumnManager="@_manager" Title="@Loc[nameof(Dashboard.Resources.StructuredLogs.StructuredLogsMessageColumnHeader)]" Tooltip="true" TooltipText="(e) => e.Message">
<AspireTemplateColumn ColumnId="@MessageColumn" ColumnManager="@_manager" Title="@Loc[nameof(Dashboard.Resources.StructuredLogs.StructuredLogsMessageColumnHeader)]">
@* Tooltip is displayed by the message GridValue instance *@
<LogMessageColumnDisplay FilterText="@(ViewModel.FilterText)" LogEntry="@context" />
</AspireTemplateColumn>
<AspireTemplateColumn ColumnId="@TraceColumn" ColumnManager="@_manager" Title="@Loc[nameof(Dashboard.Resources.StructuredLogs.StructuredLogsTraceColumnHeader)]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
@using Aspire.Dashboard.Extensions
@using Aspire.Dashboard.Otlp.Model
@using Aspire.Dashboard.Resources
@using Aspire.Dashboard.Utils

@inject IStringLocalizer<StructuredLogs> Loc

<GridValue Value="@LogEntry.Message"
<GridValue Value="@FormatHelpers.TruncateText(LogEntry.Message, FormatHelpers.ColumnMaximumTextLength)"
ValueDescription="@Loc[nameof(StructuredLogs.StructuredLogsMessageColumnHeader)]"
ToolTip="@FormatHelpers.TruncateText(LogEntry.Message, FormatHelpers.TooltipMaximumTextLength)"
EnableHighlighting="true"
HighlightText="@FilterText"
StopClickPropagation="true">
Expand Down
20 changes: 20 additions & 0 deletions src/Aspire.Dashboard/Utils/FormatHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public enum MillisecondsDisplay

internal static partial class FormatHelpers
{
// Limit size of very long data that is written in large grids.
public const int ColumnMaximumTextLength = 250;
public const int TooltipMaximumTextLength = 1500;
public const string Ellipsis = "…";

// There are an unbound number of CultureInfo instances so we don't want to use it as the key.
// Someone could have also customized their culture so we don't want to use the name as the key.
// This struct contains required information from the culture that is used in cached format strings.
Expand Down Expand Up @@ -133,4 +138,19 @@ public static string FormatNumberWithOptionalDecimalPlaces(double value, int max
};
return value.ToString(formatString, provider ?? CultureInfo.CurrentCulture);
}

public static string TruncateText(string? text, int maxLength)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

if (text.Length <= maxLength)
{
return text;
}

return string.Concat(text.AsSpan(0, maxLength - Ellipsis.Length), Ellipsis);
}
}
10 changes: 10 additions & 0 deletions tests/Aspire.Dashboard.Tests/FormatHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public void FormatDateTime_WithMilliseconds_NewZealandCulture(string expected, M
Assert.Equal(expected, FormatHelpers.FormatDateTime(CreateTimeProvider(), date, includeMilliseconds, cultureInfo: CultureInfo.GetCultureInfo("en-NZ")), ignoreWhiteSpaceDifferences: true, ignoreCase: true);
}

[Theory]
[InlineData(null, 5, "")]
[InlineData("", 5, "")]
[InlineData("abcdef", 5, "abcd" + FormatHelpers.Ellipsis)]
[InlineData("abcdef", 10, "abcdef")]
public void TruncateText(string? initialText, int maxLength, string expected)
{
Assert.Equal(expected, FormatHelpers.TruncateText(initialText, maxLength: maxLength));
}

private static DateTime GetLocalDateTime(string value)
{
Assert.True(DateTime.TryParseExact(value, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var date));
Expand Down
Loading