mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-08-06 00:02:27 +08:00
wip: 🧠 初步的框架
This commit is contained in:
@ -0,0 +1,46 @@
|
||||
using NetAdmin.Domain.Dto.Sys.RequestLog;
|
||||
using NetAdmin.Domain.Dto.Sys.User;
|
||||
using NetAdmin.Domain.Events.Sys;
|
||||
using NetAdmin.SysComponent.Application.Services.Sys.Dependency;
|
||||
|
||||
namespace NetAdmin.SysComponent.Host.Subscribers;
|
||||
|
||||
/// <summary>
|
||||
/// 操作日志记录
|
||||
/// </summary>
|
||||
public sealed class OperationLogger : IEventSubscriber
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存请求日志到数据库
|
||||
/// </summary>
|
||||
[EventSubscribe(nameof(RequestLogEvent))]
|
||||
public async Task OperationEventDbRecordAsync(EventHandlerExecutingContext context)
|
||||
{
|
||||
if (context.Source is not RequestLogEvent operationEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 跳过心跳请求
|
||||
if (operationEvent.Data.ApiId.Equals("api/health/check", StringComparison.OrdinalIgnoreCase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
CreateRequestLogReq logReq = null;
|
||||
|
||||
// 登录日志特殊处理
|
||||
if (operationEvent.Data.ApiId.Equals("api/user/login", StringComparison.OrdinalIgnoreCase)) {
|
||||
try {
|
||||
var loginReq = operationEvent.Data.RequestBody.ToObject<LoginByPwdReq>();
|
||||
logReq = operationEvent.Data with { ExtraData = loginReq.Account };
|
||||
}
|
||||
catch {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
logReq ??= operationEvent.Data;
|
||||
var logService = App.GetService<IRequestLogService>();
|
||||
logReq.TruncateStrings();
|
||||
_ = await logService.CreateAsync(logReq);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user