88 lines
2.9 KiB
C#
88 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using RobotNet.RobotManager.Services;
|
|
|
|
namespace RobotNet.RobotManager.Controllers;
|
|
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[AllowAnonymous]
|
|
public class RobotManagerLoggerController(LoggerController<RobotManagerLoggerController> Logger) : ControllerBase
|
|
{
|
|
private readonly string LoggerDirectory = "robotManagerlogs";
|
|
private readonly string OpenACSLoggerDirectory = "openACSlogs";
|
|
|
|
[HttpGet]
|
|
public async Task<IEnumerable<string>> 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.Warning($"GetLogs: phát hiện đường dẫn không hợp lệ.");
|
|
return [];
|
|
}
|
|
|
|
if (!System.IO.File.Exists(path))
|
|
{
|
|
Logger.Warning($"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.Warning($"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);
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("open-acs")]
|
|
public async Task<IEnumerable<string>> GetLogsACS([FromQuery(Name = "date")] DateTime date)
|
|
{
|
|
string temp = "";
|
|
try
|
|
{
|
|
string fileName = $"{date:yyyy-MM-dd}.log";
|
|
string path = Path.Combine(OpenACSLoggerDirectory, fileName);
|
|
if (!Path.GetFullPath(path).StartsWith(Path.GetFullPath(OpenACSLoggerDirectory)))
|
|
{
|
|
Logger.Warning($"GetLogs: phát hiện đường dẫn không hợp lệ.");
|
|
return [];
|
|
}
|
|
|
|
if (!System.IO.File.Exists(path))
|
|
{
|
|
Logger.Warning($"GetLogs: không tìm thấy file log của ngày {date.ToShortDateString()} - {path}.");
|
|
return [];
|
|
}
|
|
|
|
temp = Path.Combine(OpenACSLoggerDirectory, $"{Guid.NewGuid()}.log");
|
|
System.IO.File.Copy(path, temp);
|
|
|
|
return await System.IO.File.ReadAllLinesAsync(temp);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"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);
|
|
}
|
|
}
|
|
}
|