65 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
using Azure.Core;
 | 
						|
using LY.App.Extensions.DI;
 | 
						|
using LY.App.Model;
 | 
						|
using Mapster;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
using SqlSugar;
 | 
						|
using System.Security.AccessControl;
 | 
						|
using System.Text;
 | 
						|
 | 
						|
namespace LY.App.Service
 | 
						|
{
 | 
						|
    [ServiceInjection(InjectionType.Transient)]
 | 
						|
    public class LogService
 | 
						|
    {
 | 
						|
        private readonly SqlSugarClient _db;
 | 
						|
        public LogService(SqlSugarClient sqlSugarClient)
 | 
						|
        {
 | 
						|
            _db = sqlSugarClient;
 | 
						|
        }
 | 
						|
        public async Task AddLog(AddLog input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<LogEntity>();
 | 
						|
            await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 分页查询
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="pageNum"></param>
 | 
						|
        /// <param name="pageSize"></param>
 | 
						|
        /// <param name="key"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> List(int pageNum, int pageSize, string key)
 | 
						|
        {
 | 
						|
            RefAsync<int> total = 0;
 | 
						|
            var items = await _db.Queryable<LogEntity>()
 | 
						|
                .WhereIF(!string.IsNullOrEmpty(key), s => s.Message.Contains(key))
 | 
						|
                .OrderByDescending(s => s.Id)
 | 
						|
                .ToPageListAsync(pageNum, pageSize, total);
 | 
						|
            return new ApiResult()
 | 
						|
            {
 | 
						|
                data = new
 | 
						|
                {
 | 
						|
                    total = total.Value,
 | 
						|
                    items
 | 
						|
                }
 | 
						|
            };
 | 
						|
        }
 | 
						|
        public async Task<byte[]> DownLog(string key)
 | 
						|
        {
 | 
						|
            var query = await _db.Queryable<LogEntity>()
 | 
						|
              .WhereIF(!string.IsNullOrEmpty(key), s => s.Message.Contains(key))
 | 
						|
              .OrderByDescending(s => s.CreateTime).ToListAsync();
 | 
						|
            // 构建文本内容
 | 
						|
            var sb = new StringBuilder();
 | 
						|
            foreach (var log in query)
 | 
						|
            {
 | 
						|
                sb.AppendLine($"时间:[{log.CreateTime}] -消息:[{log.Message}]-  参数: {log.Parameters}");
 | 
						|
            }
 | 
						|
            // 将文本内容转为字节数组
 | 
						|
            byte[] fileBytes = Encoding.UTF8.GetBytes(sb.ToString());
 | 
						|
            return fileBytes;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |