日志导出功能
This commit is contained in:
parent
272b8c9142
commit
328c48dfcc
|
|
@ -1,6 +1,7 @@
|
|||
using LY.App.Service;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace LY.App.Controllers
|
||||
{
|
||||
|
|
@ -30,5 +31,16 @@ namespace LY.App.Controllers
|
|||
var result = await _logService.List(pageNum, pageSize, key);
|
||||
return Ok(result);
|
||||
}
|
||||
/// <summary>
|
||||
/// 导出日志
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("export")]
|
||||
public async Task<IActionResult> Export(string key = null)
|
||||
{
|
||||
var result = await _logService.DownLog(key);
|
||||
return File(result, "text/plain", $"{DateTime.Now.ToString("yyyy-MM-dd")}__logfile.txt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
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
|
||||
{
|
||||
|
|
@ -43,5 +45,20 @@ namespace LY.App.Service
|
|||
}
|
||||
};
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue