From 6d6caa78527ba3465d3c01c67b38f9af968e47f7 Mon Sep 17 00:00:00 2001 From: Ihor Sysak Date: Mon, 17 Jul 2023 17:22:38 +0300 Subject: [PATCH 1/2] Added the possibility to add custom headers to the error grid page and appropriately hide default headers. Also added the ability to filter errors on this page. --- .../Internal/ExceptionalSettingsBase.cs | 33 +++++ .../Pages/ErrorListPage.cs | 115 ++++++++++++------ 2 files changed, 109 insertions(+), 39 deletions(-) diff --git a/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs b/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs index 0709ea0d..cc84d0f4 100644 --- a/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs +++ b/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs @@ -99,6 +99,39 @@ public void Register(IErrorNotifier notifier) /// public Action> GetCustomData { get; set; } + /// + /// Method of filtering errors; will be called when the error list page is loading. + /// + public Func FilterDataInErrorGrid { get; set; } + + /// + /// Headers to add to the error list grid. + /// + public List AddedHeaders { get; set; } = new List(); + + /// + /// Method to add custom headers to the error list grid. + /// + /// + public void AddCustomDataToErrorGrid(params string[] headers) + { + AddedHeaders.AddRange(headers); + } + + /// + /// Headers to hide from the error list grid. + /// + public List ExcludedHeaders { get; set; } = new List(); + + /// + /// Method to hide default headers from the error list grid. + /// + /// + public void HideDefaultHeadersFromErrorGrid(params string[] headers) + { + ExcludedHeaders.AddRange(headers); + } + /// /// Settings for the rendering of pages. /// diff --git a/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs b/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs index 7def1cb2..5cd47183 100644 --- a/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs +++ b/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs @@ -1,4 +1,5 @@ using StackExchange.Exceptional.Internal; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -22,7 +23,14 @@ public class ErrorListPage : WebPage public ErrorListPage(ErrorStore store, ExceptionalSettingsBase settings, string baseURL, List errors) : base(null, settings, store, baseURL, "Error Log") { - Errors = errors.OrderByDescending(e => e.LastLogDate ?? e.CreationDate).ToList(); + if (Settings.FilterDataInErrorGrid != null) + { + Errors = errors.Where(settings.FilterDataInErrorGrid).ToList(); + } + else + { + Errors = errors.OrderByDescending(e => e.LastLogDate ?? e.CreationDate).ToList(); + } } /// @@ -64,6 +72,10 @@ protected override void RenderInnerHtml(StringBuilder sb) } else { + var headers = new List() + { + "Type", "Error", "Url", "Remote IP", "Time", "Site", "Server" + }; var last = Errors.FirstOrDefault(); // oh the irony sb.Append("

") .Append("") @@ -77,17 +89,55 @@ protected override void RenderInnerHtml(StringBuilder sb) .AppendLine(@" - - - - - - - - - + "); + + headers = headers.Concat(Settings.AddedHeaders).Distinct().Except(Settings.ExcludedHeaders).ToList(); + + foreach (var header in headers) + { + sb.AppendLine($" "); + } + + var displayLogic = new Dictionary>() + { + { "Type", (e, sb) => sb.Append(" ") }, + { "Error", (e, sb) => { + sb.Append(" "); + } }, + { "Url", (e, sb) => { + sb.Append(" "); + } }, + { "Remote IP", (e, sb) => sb.Append(" ") }, + { "Time", (e, sb) => sb.Append(" ") }, + { "Site", (e, sb) => sb.Append(" ") }, + { "Server", (e, sb) => sb.Append(" ") }, + }; + + sb.AppendLine(@" "); + foreach (var e in Errors) { sb.Append(" ") @@ -101,36 +151,23 @@ protected override void RenderInnerHtml(StringBuilder sb) { sb.Append(" ").Append(IconLock).Append(""); } - sb.AppendLine("") - .Append(" ") - .Append(" ") - .Append(" "); + + foreach (var header in headers) { - sb.Append("") - .Append(e.UrlPath.EncodeTruncateWithEllipsis(40)) - .Append(""); + if (displayLogic.TryGetValue(header, out var displayAction)) + { + displayAction.Invoke(e, sb); + } + else if (e.CustomData != null && e.CustomData.TryGetValue(header, out var customValue)) + { + sb.Append(" "); + } + else + { + sb.Append(" "); + } } - sb.AppendLine("") - .Append(" ") - .Append(" ") - .Append(" ") - .Append(" ") - .AppendLine(" "); } sb.AppendLine(" ") .AppendLine("
TypeErrorUrlRemote IPTimeSiteServer
{header}") + .AppendHtmlEncode(e.Type.ToShortTypeName()) + .AppendLine("").Append(e.Message.HasValue() ? e.Message.EncodeTruncateWithEllipsis(250) : "(no message)").Append(""); + if (e.DuplicateCount > 1) + { + sb.Append(" (") + .Append(e.DuplicateCount.ToString()) + .Append(")"); + } + sb.AppendLine(""); + if (e.UrlPath.HasValue()) + { + sb.Append("") + .Append(e.UrlPath.EncodeTruncateWithEllipsis(40)) + .Append(""); + } + sb.AppendLine("").AppendHtmlEncode(e.IPAddress).AppendLine("") + .AppendHtmlEncode((e.LastLogDate ?? e.CreationDate).ToRelativeTime()) + .AppendLine("").AppendHtmlEncode(e.Host).AppendLine("").AppendHtmlEncode(e.MachineName).AppendLine("
") - .AppendHtmlEncode(e.Type.ToShortTypeName()) - .AppendLine("").Append(e.Message.HasValue() ? e.Message.EncodeTruncateWithEllipsis(250) : "(no message)").Append(""); - if (e.DuplicateCount > 1) - { - sb.Append(" (") - .Append(e.DuplicateCount.ToString()) - .Append(")"); - } - sb.AppendLine(""); - if (e.UrlPath.HasValue()) + sb.AppendLine("").AppendHtmlEncode(customValue).AppendLine("").AppendHtmlEncode(e.IPAddress).AppendLine("") - .AppendHtmlEncode((e.LastLogDate ?? e.CreationDate).ToRelativeTime()) - .AppendLine("").AppendHtmlEncode(e.Host).AppendLine("").AppendHtmlEncode(e.MachineName).AppendLine("
"); @@ -145,4 +182,4 @@ protected override void RenderInnerHtml(StringBuilder sb) } } } -} \ No newline at end of file +} From 7d4105fb3347a8f8cb7f904268eb53a62f65ed6d Mon Sep 17 00:00:00 2001 From: Ihor Sysak Date: Tue, 18 Jul 2023 14:09:33 +0300 Subject: [PATCH 2/2] removed useless methods --- .../Internal/ExceptionalSettingsBase.cs | 31 +++---------------- .../Pages/ErrorListPage.cs | 2 +- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs b/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs index cc84d0f4..2da3b66a 100644 --- a/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs +++ b/src/StackExchange.Exceptional.Shared/Internal/ExceptionalSettingsBase.cs @@ -100,37 +100,16 @@ public void Register(IErrorNotifier notifier) public Action> GetCustomData { get; set; } /// - /// Method of filtering errors; will be called when the error list page is loading. + /// Method to filter errors. + /// The method takes an `Error` object as input and returns a boolean value indicating whether the object should be filtered. /// public Func FilterDataInErrorGrid { get; set; } /// - /// Headers to add to the error list grid. + /// Method to get the default columns for a list of strings. + /// The list of strings can be manipulated to remove or add columns. /// - public List AddedHeaders { get; set; } = new List(); - - /// - /// Method to add custom headers to the error list grid. - /// - /// - public void AddCustomDataToErrorGrid(params string[] headers) - { - AddedHeaders.AddRange(headers); - } - - /// - /// Headers to hide from the error list grid. - /// - public List ExcludedHeaders { get; set; } = new List(); - - /// - /// Method to hide default headers from the error list grid. - /// - /// - public void HideDefaultHeadersFromErrorGrid(params string[] headers) - { - ExcludedHeaders.AddRange(headers); - } + public Action> GetColumns { get; set; } /// /// Settings for the rendering of pages. diff --git a/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs b/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs index 5cd47183..81305927 100644 --- a/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs +++ b/src/StackExchange.Exceptional.Shared/Pages/ErrorListPage.cs @@ -91,7 +91,7 @@ protected override void RenderInnerHtml(StringBuilder sb) "); - headers = headers.Concat(Settings.AddedHeaders).Distinct().Except(Settings.ExcludedHeaders).ToList(); + Settings.GetColumns?.Invoke(headers); foreach (var header in headers) {