Files
DMS/DMS.Application/Services/Management/LogManagementService.cs

93 lines
2.6 KiB
C#
Raw Normal View History

2025-09-07 21:16:56 +08:00
using DMS.Application.DTOs;
2025-09-16 14:42:23 +08:00
using DMS.Application.Events;
2025-09-07 21:16:56 +08:00
using DMS.Application.Interfaces;
2025-09-16 12:29:09 +08:00
using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces.Management;
using DMS.Core.Enums;
2025-09-07 21:16:56 +08:00
2025-09-16 12:29:09 +08:00
namespace DMS.Application.Services.Management;
2025-09-07 21:16:56 +08:00
/// <summary>
/// 日志管理服务,负责日志相关的业务逻辑。
/// </summary>
public class LogManagementService : ILogManagementService
2025-09-07 21:16:56 +08:00
{
private readonly INlogAppService _nlogAppService;
private readonly IAppStorageService _appStorageService;
2025-09-07 21:16:56 +08:00
/// <summary>
/// 当日志数据发生变化时触发
/// </summary>
public event EventHandler<NlogChangedEventArgs> OnLogChanged;
2025-09-07 21:16:56 +08:00
public LogManagementService(INlogAppService nlogAppService,IAppStorageService appStorageService)
2025-09-07 21:16:56 +08:00
{
_nlogAppService = nlogAppService;
_appStorageService = appStorageService;
2025-09-07 21:16:56 +08:00
}
/// <summary>
/// 异步根据ID获取日志DTO。
/// </summary>
public async Task<NlogDto> GetNlogByIdAsync(int id)
{
return await _nlogAppService.GetLogByIdAsync(id);
}
/// <summary>
/// 异步获取所有日志DTO列表。
/// </summary>
public async Task<List<NlogDto>> GetAllNlogsAsync()
{
return await _nlogAppService.GetAllLogsAsync();
}
/// <summary>
/// 异步获取指定数量的最新日志DTO列表。
/// </summary>
public async Task<List<NlogDto>> GetLatestNlogsAsync(int count)
{
return await _nlogAppService.GetLatestLogsAsync(count);
}
/// <summary>
/// 异步清空所有日志。
/// </summary>
public async Task ClearAllNlogsAsync()
{
await _nlogAppService.ClearAllLogsAsync();
}
/// <summary>
/// 在内存中添加日志
/// </summary>
public void AddNlogToMemory(NlogDto nlogDto)
{
if (_appStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto))
2025-09-07 21:16:56 +08:00
{
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Added, nlogDto));
2025-09-07 21:16:56 +08:00
}
}
/// <summary>
/// 在内存中更新日志
/// </summary>
public void UpdateNlogInMemory(NlogDto nlogDto)
{
_appStorageService.Nlogs.AddOrUpdate(nlogDto.Id, nlogDto, (key, oldValue) => nlogDto);
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Updated, nlogDto));
2025-09-07 21:16:56 +08:00
}
/// <summary>
/// 在内存中删除日志
/// </summary>
public void RemoveNlogFromMemory(int nlogId)
{
if (_appStorageService.Nlogs.TryRemove(nlogId, out var nlogDto))
2025-09-07 21:16:56 +08:00
{
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Deleted, nlogDto));
2025-09-07 21:16:56 +08:00
}
}
2025-09-07 21:16:56 +08:00
}