Skip to content

Commit

Permalink
Merge pull request #3 from jrsearles/feat/simplify-log-models
Browse files Browse the repository at this point in the history
feat: simplify logging model
  • Loading branch information
jrsearles authored Jan 11, 2024
2 parents 7936ac9 + a253756 commit 6fd980c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
40 changes: 33 additions & 7 deletions src/Elmah.AspNetCore/ErrorWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,25 +312,24 @@ public string? Client
public string? Version => _error.ServerVariables["Version"];

[XmlIgnore]
public List<IElmahLogMessage> MessageLog
public List<ErrorLogMessageWrapper> MessageLog
{
get => GetMessageLog();
// ReSharper disable once ValueParameterNotUsed
set { }
}

private List<IElmahLogMessage> GetMessageLog()
private List<ErrorLogMessageWrapper> 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();
Expand Down Expand Up @@ -452,4 +451,31 @@ public List<ElmahLogParamEntry> Params
// ReSharper disable once ValueParameterNotUsed
set { }
}

internal sealed class ErrorLogMessageWrapper
{
private readonly IElmahLogMessage _message;
private readonly KeyValuePair<string, string>[]? _parameters;

public ErrorLogMessageWrapper(IElmahLogMessage message, KeyValuePair<string, string>[]? 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<string, string>[]? Params => _parameters;

// This property is used on client side only - passing it down as initial state
public bool Collapsed => true;
}
}
2 changes: 1 addition & 1 deletion src/Elmah.AspNetCore/Handlers/ErrorResourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Elmah.AspNetCore.Handlers;
internal static partial class Endpoints
{
private static readonly HashSet<string> 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.";
Expand Down
2 changes: 1 addition & 1 deletion src/Elmah.AspNetCore/Logger/ElmahLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
}

var feature = _accessor.HttpContext!.Features.Get<IElmahLogFeature>()!;
var entry = new ElmahLogMessageEntry<TState>
var entry = new ElmahLoggerMessage<TState>
{
TimeStamp = DateTime.Now,
State = state,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Elmah;
namespace Elmah.AspNetCore.Logger;

public sealed class ElmahLogMessageEntry<TState> : IElmahLogMessage
internal sealed class ElmahLoggerMessage<TState> : IElmahLogMessage
{
public DateTime TimeStamp { get; init; }
public string? Scope { get; init; }
Expand Down
3 changes: 0 additions & 3 deletions src/Elmah.Common/IElmahLogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ public interface IElmahLogMessage
string? Exception { get; }
string? Scope { get; }
LogLevel? Level { get; }
string? Message => this.Render();
string? Render();
KeyValuePair<string, string>[]? Params => null;
bool Collapsed => true;
}
2 changes: 1 addition & 1 deletion src/Elmah.Common/Xml/ErrorXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private static void WriteMessageLog(XmlWriter writer, string name, IEnumerable<I
WriteXmlAttribute(writer, "time-stamp",
XmlConvert.ToString(entry.TimeStamp.ToUniversalTime(), @"yyyy-MM-dd\THH:mm:ss.fffffff\Z"));
WriteXmlAttribute(writer, "scope", entry.Scope);
WriteXmlAttribute(writer, "message", entry.Message);
WriteXmlAttribute(writer, "message", entry.Render());
writer.WriteEndElement();
}

Expand Down
3 changes: 0 additions & 3 deletions src/Elmah.Common/XmlLogMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Elmah;
Expand All @@ -16,7 +15,5 @@ public sealed class XmlLogMessage : IElmahLogMessage

public string? Message { get; set; }

public KeyValuePair<string, string>[] Params { get; set; } = Array.Empty<KeyValuePair<string, string>>();

public string? Render() => this.Message;
}

0 comments on commit 6fd980c

Please sign in to comment.