using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace RobotNet.IdentityServer.Controllers; [Route("api/[controller]")] [ApiController] [AllowAnonymous] public class IdentityServerLoggerController(ILogger Logger) : ControllerBase { private readonly string LoggerDirectory = "identityServerlogs"; [HttpGet] public async Task> GetLogs([FromQuery(Name = "date")] DateTime date) { string temp = ""; try { string fileName = $"{date:yyyy-MM-dd}.log"; string path = Path.Combine(LoggerDirectory, fileName); if (!Path.GetFullPath(path).StartsWith(Path.GetFullPath(LoggerDirectory))) { Logger.LogWarning($"GetLogs: phát hiện đường dẫn không hợp lệ."); return []; } if (!System.IO.File.Exists(path)) { Logger.LogWarning($"GetLogs: không tìm thấy file log của ngày {date.ToShortDateString()} - {path}."); return []; } temp = Path.Combine(LoggerDirectory, $"{Guid.NewGuid()}.log"); System.IO.File.Copy(path, temp); return await System.IO.File.ReadAllLinesAsync(temp); } catch (Exception ex) { Logger.LogWarning($"GetLogs: Hệ thống có lỗi xảy ra - {ex.Message}"); return []; } finally { if (System.IO.File.Exists(temp)) System.IO.File.Delete(temp); } } }