feat: slightly more debug logs, enrich message events with extra data
This commit is contained in:
parent
c06376dfda
commit
27e77eeaed
5 changed files with 79 additions and 5 deletions
58
Catalogger.Backend/Extensions/LogUtils.cs
Normal file
58
Catalogger.Backend/Extensions/LogUtils.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using Remora.Discord.API.Abstractions.Gateway.Events;
|
||||
using Remora.Rest.Core;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace Catalogger.Backend.Extensions;
|
||||
|
||||
public static class LogUtils
|
||||
{
|
||||
public static IDisposable Enrich<T>(T evt)
|
||||
where T : IGatewayEvent
|
||||
{
|
||||
var type = ("Event", typeof(T).Name);
|
||||
|
||||
return evt switch
|
||||
{
|
||||
IMessageDelete md => PushProperties(
|
||||
type,
|
||||
("GuildId", md.GuildID),
|
||||
("ChannelId", md.ChannelID),
|
||||
("MessageId", md.ID)
|
||||
),
|
||||
IMessageUpdate mc => PushProperties(
|
||||
type,
|
||||
("GuildId", mc.GuildID),
|
||||
("ChannelId", mc.ChannelID),
|
||||
("MessageId", mc.ID)
|
||||
),
|
||||
IMessageDeleteBulk mdb => PushProperties(
|
||||
type,
|
||||
("GuildId", mdb.GuildID),
|
||||
("ChannelId", mdb.ChannelID),
|
||||
("MessageIds", mdb.IDs)
|
||||
),
|
||||
_ => PushProperties(type),
|
||||
};
|
||||
}
|
||||
|
||||
public static IDisposable PushProperties(params (string, object?)[] properties) =>
|
||||
new MultiDisposable(
|
||||
properties
|
||||
.Select(p =>
|
||||
{
|
||||
if (p.Item2 is Optional<Snowflake> s)
|
||||
return LogContext.PushProperty(p.Item1, s.IsDefined() ? s.Value : null);
|
||||
return LogContext.PushProperty(p.Item1, p.Item2);
|
||||
})
|
||||
.ToArray()
|
||||
);
|
||||
|
||||
private record MultiDisposable(IDisposable[] Entries) : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var e in Entries)
|
||||
e.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue