From a25375607e5513ba7cabeb2a006fe5726546b13b Mon Sep 17 00:00:00 2001 From: Joshua Searles Date: Wed, 10 Jan 2024 22:55:42 -0500 Subject: [PATCH] feat: simplify logging model --- src/Elmah.AspNetCore/ErrorWrapper.cs | 40 +++++++++++++++---- .../Handlers/ErrorResourceHandler.cs | 2 +- src/Elmah.AspNetCore/Logger/ElmahLogger.cs | 2 +- .../Logger/ElmahLoggerMessage.cs} | 5 +-- src/Elmah.Common/IElmahLogMessage.cs | 3 -- src/Elmah.Common/Xml/ErrorXml.cs | 2 +- src/Elmah.Common/XmlLogMessage.cs | 3 -- 7 files changed, 38 insertions(+), 19 deletions(-) rename src/{Elmah.Common/ElmahLogMessageEntry.cs => Elmah.AspNetCore/Logger/ElmahLoggerMessage.cs} (81%) diff --git a/src/Elmah.AspNetCore/ErrorWrapper.cs b/src/Elmah.AspNetCore/ErrorWrapper.cs index f80fe1d..5737445 100644 --- a/src/Elmah.AspNetCore/ErrorWrapper.cs +++ b/src/Elmah.AspNetCore/ErrorWrapper.cs @@ -312,25 +312,24 @@ public string? Client public string? Version => _error.ServerVariables["Version"]; [XmlIgnore] - public List MessageLog + public List MessageLog { get => GetMessageLog(); // ReSharper disable once ValueParameterNotUsed set { } } - private List GetMessageLog() + private List GetMessageLog() { - var result = _error.MessageLog.ToList(); + var result = _error.MessageLog.Select(x => new ErrorLogMessageWrapper(x)).ToList(); foreach (var param in _error.Params) { - result.Add(new XmlLogMessage + result.Add(new ErrorLogMessageWrapper(new XmlLogMessage { TimeStamp = param.TimeStamp, Level = LogLevel.Information, - Message = $"Method {param.TypeName}.{param.MemberName} call with parameters:", - Params = param.Params - }); + Message = $"Method {param.TypeName}.{param.MemberName} call with parameters:" + }, param.Params)); } return result.OrderBy(i => i.TimeStamp).ToList(); @@ -452,4 +451,31 @@ public List Params // ReSharper disable once ValueParameterNotUsed set { } } + + internal sealed class ErrorLogMessageWrapper + { + private readonly IElmahLogMessage _message; + private readonly KeyValuePair[]? _parameters; + + public ErrorLogMessageWrapper(IElmahLogMessage message, KeyValuePair[]? parameters = null) + { + _message = message; + _parameters = parameters; + } + + public DateTime TimeStamp => _message.TimeStamp; + + public string? Exception => _message.Exception; + + public string? Scope => _message.Scope; + + public LogLevel? Level => _message.Level; + + public string? Message => _message.Render(); + + public KeyValuePair[]? Params => _parameters; + + // This property is used on client side only - passing it down as initial state + public bool Collapsed => true; + } } \ No newline at end of file diff --git a/src/Elmah.AspNetCore/Handlers/ErrorResourceHandler.cs b/src/Elmah.AspNetCore/Handlers/ErrorResourceHandler.cs index 96d1d7e..aa09ace 100644 --- a/src/Elmah.AspNetCore/Handlers/ErrorResourceHandler.cs +++ b/src/Elmah.AspNetCore/Handlers/ErrorResourceHandler.cs @@ -16,7 +16,7 @@ namespace Elmah.AspNetCore.Handlers; internal static partial class Endpoints { private static readonly HashSet ResourceNames = - new(typeof(Endpoints).GetTypeInfo().Assembly.GetManifestResourceNames(), StringComparer.OrdinalIgnoreCase); + new(typeof(Endpoints).Assembly.GetManifestResourceNames(), StringComparer.OrdinalIgnoreCase); private static readonly Assembly ThisAssembly = typeof(Endpoints).Assembly; private static readonly string ResourcePrefix = $"{ThisAssembly.GetName().Name}.wwwroot."; diff --git a/src/Elmah.AspNetCore/Logger/ElmahLogger.cs b/src/Elmah.AspNetCore/Logger/ElmahLogger.cs index 95d861c..9fce762 100644 --- a/src/Elmah.AspNetCore/Logger/ElmahLogger.cs +++ b/src/Elmah.AspNetCore/Logger/ElmahLogger.cs @@ -41,7 +41,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } var feature = _accessor.HttpContext!.Features.Get()!; - var entry = new ElmahLogMessageEntry + var entry = new ElmahLoggerMessage { TimeStamp = DateTime.Now, State = state, diff --git a/src/Elmah.Common/ElmahLogMessageEntry.cs b/src/Elmah.AspNetCore/Logger/ElmahLoggerMessage.cs similarity index 81% rename from src/Elmah.Common/ElmahLogMessageEntry.cs rename to src/Elmah.AspNetCore/Logger/ElmahLoggerMessage.cs index 816ab99..c2768ee 100644 --- a/src/Elmah.Common/ElmahLogMessageEntry.cs +++ b/src/Elmah.AspNetCore/Logger/ElmahLoggerMessage.cs @@ -1,10 +1,9 @@ using System; -using System.Collections.Generic; using Microsoft.Extensions.Logging; -namespace Elmah; +namespace Elmah.AspNetCore.Logger; -public sealed class ElmahLogMessageEntry : IElmahLogMessage +internal sealed class ElmahLoggerMessage : IElmahLogMessage { public DateTime TimeStamp { get; init; } public string? Scope { get; init; } diff --git a/src/Elmah.Common/IElmahLogMessage.cs b/src/Elmah.Common/IElmahLogMessage.cs index 8c22bb1..c645d0d 100644 --- a/src/Elmah.Common/IElmahLogMessage.cs +++ b/src/Elmah.Common/IElmahLogMessage.cs @@ -10,8 +10,5 @@ public interface IElmahLogMessage string? Exception { get; } string? Scope { get; } LogLevel? Level { get; } - string? Message => this.Render(); string? Render(); - KeyValuePair[]? Params => null; - bool Collapsed => true; } \ No newline at end of file diff --git a/src/Elmah.Common/Xml/ErrorXml.cs b/src/Elmah.Common/Xml/ErrorXml.cs index 29e2e78..ad9ac32 100644 --- a/src/Elmah.Common/Xml/ErrorXml.cs +++ b/src/Elmah.Common/Xml/ErrorXml.cs @@ -369,7 +369,7 @@ private static void WriteMessageLog(XmlWriter writer, string name, IEnumerable[] Params { get; set; } = Array.Empty>(); - public string? Render() => this.Message; }