58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
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);
|
||
}
|
||
}
|