Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify logging model #3

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}