fix 优化操作日志

This commit is contained in:
2025-03-04 10:27:20 +08:00
parent 3e0752d852
commit 2f32885663
3 changed files with 33 additions and 12 deletions

View File

@@ -1,25 +1,46 @@
using Dlhis.Entity.Common.Entities;
using Dlhis.Entity.Common.Enums;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
namespace Dlhis.Basic.Common.ApiLog.Services.Implements;
public class OperateLogService : IOperateLog
{
private readonly ICurrentUser _currentUser;
private readonly ILogger<OperateLogService> _logger;
private readonly IDbContext _dbContext;
public OperateLogService(IDbContext dbContext)
public OperateLogService(IDbContext dbContext, ICurrentUser currentUser, ILogger<OperateLogService> logger)
{
_currentUser = currentUser;
_logger = logger;
_dbContext = dbContext.ChangeDb("log");
}
/// <inheritdoc />
public async Task LogAsync(OperateLogType type, string content)
{
await _dbContext.InsertAsync(new OperateLogEntity
var now = DateTime.Now;
var userId = _currentUser.GetIntSubjectId();
var userName = _currentUser.GetName();
try
{
Type = type,
Content = content
});
await _dbContext.InsertAsync(new OperateLogEntity
{
Type = type,
Content = content,
Operator = userName,
CreateTime = now,
ModifiedTime = now,
CreatedBy = userId,
ModifiedBy = userId
});
}
catch (Exception e)
{
_logger.LogError(e, "记录操作日志失败:{error}", e.Message);
}
}
}