From 095df4f802c5d51882388928999834e4f894357c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 18 Apr 2024 22:11:46 +0200 Subject: [PATCH] Improve display of job result and properties 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. --- src/Hangfire.Core/Common/StringExtensions.cs | 29 +++++++++++++++++++ .../Dashboard/JobHistoryRenderer.cs | 4 +-- .../Dashboard/Pages/JobDetailsPage.cshtml | 2 +- .../Dashboard/Pages/JobDetailsPage.cshtml.cs | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/Hangfire.Core/Common/StringExtensions.cs diff --git a/src/Hangfire.Core/Common/StringExtensions.cs b/src/Hangfire.Core/Common/StringExtensions.cs new file mode 100644 index 000000000..07c8c4463 --- /dev/null +++ b/src/Hangfire.Core/Common/StringExtensions.cs @@ -0,0 +1,29 @@ +using System; + +namespace Hangfire.Common +{ + internal static class StringExtensions + { + /// + /// Returns the deserialized JSON if the text is a serialized JSON string, + /// or the specified text without any transformation if the text is not a serialized JSON string. + /// + /// The text to prettify. + public static string PrettyJsonString(this string text) + { + if (text.StartsWith("\"", StringComparison.Ordinal)) + { + try + { + return SerializationHelper.Deserialize(text, SerializationOption.User); + } + catch + { + // Ignore + } + } + + return text; + } + } +} \ No newline at end of file diff --git a/src/Hangfire.Core/Dashboard/JobHistoryRenderer.cs b/src/Hangfire.Core/Dashboard/JobHistoryRenderer.cs index a36d0a330..0176aa391 100644 --- a/src/Hangfire.Core/Dashboard/JobHistoryRenderer.cs +++ b/src/Hangfire.Core/Dashboard/JobHistoryRenderer.cs @@ -195,8 +195,8 @@ public static NonEscapedString SucceededRenderer(HtmlHelper html, IDictionaryResult:
{html.HtmlEncode(result)}
"); + var result = stateData["Result"].PrettyJsonString(); + builder.Append($"
Result:
{html.HtmlEncode(result).Replace("\n", "
")}
"); itemsAdded = true; } diff --git a/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml b/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml index 0137693a4..8e98278d6 100644 --- a/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml +++ b/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml @@ -118,7 +118,7 @@ { @parameter.Key -
@parameter.Value
+
@parameter.Value.PrettyJsonString()
} diff --git a/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml.cs b/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml.cs index f5e2afa7d..7b6ef6fcf 100644 --- a/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml.cs +++ b/src/Hangfire.Core/Dashboard/Pages/JobDetailsPage.cshtml.cs @@ -473,7 +473,7 @@ public override void Execute() #line 121 "..\..\Dashboard\Pages\JobDetailsPage.cshtml" - Write(parameter.Value); + Write(parameter.Value.PrettyJsonString()); #line default