Skip to content

Commit 45b1a33

Browse files
committed
Add truncate tag helper.
1 parent c6ebc6b commit 45b1a33

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
2+
using Microsoft.AspNetCore.Razor.TagHelpers;
3+
using System;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Serilog.Ui.Web
8+
{
9+
[HtmlTargetElement("truncate")]
10+
public class TruncateTagHelper : TagHelper
11+
{
12+
[HtmlAttributeName("length")]
13+
public int Length { get; set; }
14+
15+
[HtmlAttributeName("truncate")]
16+
public ModelExpression Truncate { get; set; }
17+
18+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
19+
{
20+
if (context == null)
21+
throw new ArgumentNullException(nameof(context));
22+
23+
if (output == null)
24+
throw new ArgumentNullException(nameof(output));
25+
26+
await base.ProcessAsync(context, output);
27+
28+
string content = null;
29+
if (Truncate != null)
30+
content = Truncate.Model?.ToString();
31+
32+
content ??= (await output.GetChildContentAsync(NullHtmlEncoder.Default)).GetContent(NullHtmlEncoder.Default);
33+
34+
if (string.IsNullOrEmpty(content))
35+
return;
36+
37+
if (content.Length <= Length)
38+
{
39+
output.Content.SetContent(content);
40+
return;
41+
}
42+
43+
var sb = new StringBuilder();
44+
sb.Append("<a href=\"#\" title=\"Click to view\" class=\"modal-trigger\" data-type=\"text\">");
45+
sb.Append(content.Substring(0, Length));
46+
sb.Append(" ...");
47+
sb.Append("<span style=\"display: none\">");
48+
sb.Append(content);
49+
sb.Append("</span></a>");
50+
output.Content.SetHtmlContent(sb.ToString());
51+
}
52+
}
53+
}

src/Serilog.Ui.Web/Views/Logs/Index.cshtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2+
@addTagHelper *, Serilog.Ui.Web
3+
24
@model Serilog.Ui.Web.ViewModel.LogViewModel
35
@{
46
ViewData["Title"] = "Log List";
@@ -16,6 +18,7 @@
1618
showingItemsEnd = showingItemsStart + Model.Count;
1719
}
1820
}
21+
1922
<h1>Log List</h1>
2023
<form method="get" asp-action="Index">
2124

@@ -85,7 +88,7 @@
8588
<td class="text-center"><span class="log-level text-white @levelClass">@log.Level</span></td>
8689
<td class="text-center">@log.Timestamp</td>
8790
<td class="log-message">
88-
<span class="overflow-auto"> @log.Message</span>
91+
<span class="overflow-auto"><truncate length="100">@log.Message</truncate></span>
8992
</td>
9093
<td class="text-center">
9194
@if (log.Exception != null)
@@ -98,7 +101,7 @@
98101
}
99102
</td>
100103
<td class="text-center">
101-
<a href="#" class="modal-trigger" title="Click to view" data-type="xml">
104+
<a href="#" class="modal-trigger" title="Click to view" data-type="@log.PropertyType">
102105
View
103106
<span style="display: none">@log.Properties</span>
104107
</a>

0 commit comments

Comments
 (0)