41 lines
1.0 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using MilliSim.Data;
using MilliSim.Common.Entities;
namespace MilliSim.Services.Infrastructure;
public interface IOperationLogService
{
Task LogAsync(string module, string operation, string detail);
}
public class OperationLogService : IOperationLogService
{
private readonly MilliSimDbContext _db;
private readonly ICurrentUserService _currentUser;
public OperationLogService(MilliSimDbContext db, ICurrentUserService currentUser)
{
_db = db;
_currentUser = currentUser;
}
public async Task LogAsync(string module, string operation, string detail)
{
var log = new OperationLog
{
UserId = _currentUser.UserId,
Username = _currentUser.Username,
Module = module,
Operation = operation,
Detail = detail,
Ip = _currentUser.Ip
};
_db.OperationLogs.Add(log);
await _db.SaveChangesAsync();
}
}