Files
WCS/Cowain.Base/Services/LogService.cs
2026-03-02 09:08:20 +08:00

58 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Cowain.Base.DBContext;
using Cowain.Base.IServices;
using Cowain.Base.Models;
using Cowain.Base.ViewModels;
using Microsoft.EntityFrameworkCore;
namespace Cowain.Base.Services;
public class LogService : BaseService, ILogService
{
public LogService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
{
}
public async Task<List<SerilogViewModel>> GetAllAsync()
{
var data = await FindAsync<SerilogDto>();
return new List<SerilogViewModel>(data.Select(x => new SerilogViewModel
{
Id = x.id,
Template = x.Template,
Message = x.Message,
Exception = x.Exception,
Properties = x.Properties,
Level = x.Level,
Timestamp = x.Timestamp
}));
}
/// <summary>
/// 分页获取日志复用BaseService的FindAsync+内存分页)
/// </summary>
public async Task<(List<SerilogViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize)
{
// 复用BaseService的异步查询方法
var allData = await FindAsync<SerilogDto>();
// 分页处理
var paginatedData = allData
.OrderByDescending(x => x.id)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.Select(x => new SerilogViewModel
{
Id = x.id,
Template = x.Template,
Message = x.Message,
Exception = x.Exception,
Properties = x.Properties,
Level = x.Level,
Timestamp = x.Timestamp
})
.ToList();
return (paginatedData, allData.Count);
}
}