Skip to content

Commit

Permalink
Improve display of job result and properties
Browse files Browse the repository at this point in the history
It's quite common that job properties and/or result are strings. Since everything is serialized as JSON in the database, a string becomes quoted and has escaped characters. For displaying in the dashboard, it's nicer without the quotes and escaped characters.
  • Loading branch information
0xced committed May 22, 2024
1 parent 863c9da commit 556ef2e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/Hangfire.Core/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Hangfire.Common
{
internal static class StringExtensions
{
/// <summary>
/// Returns the deserialized JSON <paramref name="text"/> if the text is a serialized JSON string,
/// or the specified text without any transformation if the text is not a serialized JSON string.
/// </summary>
/// <param name="text">The text to prettify.</param>
public static string PrettyJsonString(this string text)
{
if (text.StartsWith("\"", StringComparison.Ordinal))
{
try
{
return SerializationHelper.Deserialize<string>(text, SerializationOption.User);
}
catch
{
// Ignore
}
}

return text;
}
}
}
4 changes: 2 additions & 2 deletions src/Hangfire.Core/Dashboard/JobHistoryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public static NonEscapedString SucceededRenderer(HtmlHelper html, IDictionary<st

if (stateData.TryGetValue("Result", out var resultString) && !String.IsNullOrWhiteSpace(resultString))
{
var result = stateData["Result"];
builder.Append($"<dt>Result:</dt><dd>{html.HtmlEncode(result)}</dd>");
var result = stateData["Result"].PrettyJsonString();
builder.Append($"<dt>Result:</dt><dd>{html.HtmlEncode(result).Replace("\n", "<br>")}</dd>");

itemsAdded = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
{
<tr>
<td class="width-20 word-break">@parameter.Key</td>
<td><pre><code>@parameter.Value</code></pre></td>
<td><pre><code>@parameter.Value.PrettyJsonString()</code></pre></td>
</tr>
}
</table>
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public override void Execute()


#line 121 "..\..\Dashboard\Pages\JobDetailsPage.cshtml"
Write(parameter.Value);
Write(parameter.Value.PrettyJsonString());


#line default
Expand Down

0 comments on commit 556ef2e

Please sign in to comment.