302 lines
11 KiB
C#
302 lines
11 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.IO;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Core.Web.Admin.Models;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.Controllers.System
|
|
{
|
|
//=======================================================================
|
|
// System - Ajax - Logs controller
|
|
//=======================================================================
|
|
partial class SystemController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Database data.
|
|
private class LogFolderData
|
|
{
|
|
public String Name { get; set; }
|
|
}
|
|
private class LogFileData
|
|
{
|
|
public String Name { get; set; }
|
|
public String UpdateFmt { get; set; }
|
|
public long Size { get; set; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Log Folder List ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_LogFolderList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_LogFolderList(SaxModel_Admin model) : base(model) { }
|
|
// Log file list.
|
|
public List<LogFolderData> Folders { get; } = new List<LogFolderData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Retrieve the list of folders under the root folder.
|
|
/// <para>- These should correspond to applications (admin, api, online etc).</para>
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Route("System/Ajax/Logs/GetFolderList")]
|
|
[Route("System/Ajax/Logs/GetFolders")]
|
|
public IActionResult Ajax_GetLogFolderList(Model_System model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetLogFolderList";
|
|
String messageTitle = Localiser["system.caption.logs"];
|
|
AjaxResponse_LogFolderList responseData = new AjaxResponse_LogFolderList(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Identify folders in root log folder.
|
|
String rootLogFolder = model.LoggingRootFolder;
|
|
foreach (String folder in Directory.EnumerateDirectories(rootLogFolder))
|
|
{
|
|
String folderName = Path.GetFileName(folder);
|
|
if (folderName.IsMatch("^(Online|Admin|Api)$", RegexOptions.IgnoreCase))
|
|
responseData.Folders.Add(new LogFolderData() { Name = folderName });
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Log Year Folder List ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_LogYearList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_LogYearList(SaxModel_Admin model) : base(model) { }
|
|
// Log file list.
|
|
public List<LogFolderData> Years { get; } = new List<LogFolderData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Retrieve the list of year folders under the selected folder.
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Route("System/Ajax/Logs/GetYearList")]
|
|
[Route("System/Ajax/Logs/GetYears")]
|
|
public IActionResult Ajax_GetLogYearList(Model_System model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetLogYearList";
|
|
String messageTitle = Localiser["system.caption.logs"];
|
|
AjaxResponse_LogYearList responseData = new AjaxResponse_LogYearList(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Store filter properties to session.
|
|
SessionData_Admin.System.Logs.Filter.Folder = model.LogsFilter.Folder;
|
|
SessionData_Admin.System.Logs.Filter.Year = "";
|
|
SessionData_Admin.System.Logs.Filter.Month = "";
|
|
|
|
// Identify folders in selected log folder.
|
|
String logFolder = Path.Combine(model.LoggingRootFolder, model.LogsFilter.Folder);
|
|
foreach (String folder in Directory.EnumerateDirectories(logFolder))
|
|
{
|
|
String folderName = Path.GetFileName(folder);
|
|
if (folderName.IsMatch("^\\d{4}$"))
|
|
responseData.Years.Add(new LogFolderData() { Name = folderName });
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Log Month Folder List ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_LogMonthList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_LogMonthList(SaxModel_Admin model) : base(model) { }
|
|
// Log file list.
|
|
public List<LogFolderData> Months { get; } = new List<LogFolderData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Retrieve the list of year folders under the selected folder.
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Route("System/Ajax/Logs/GetMonthList")]
|
|
[Route("System/Ajax/Logs/GetMonths")]
|
|
public IActionResult Ajax_GetLogMonthList(Model_System model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetLogMonthList";
|
|
String messageTitle = Localiser["system.caption.logs"];
|
|
AjaxResponse_LogMonthList responseData = new AjaxResponse_LogMonthList(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Store filter properties to session.
|
|
SessionData_Admin.System.Logs.Filter.Folder = model.LogsFilter.Folder;
|
|
SessionData_Admin.System.Logs.Filter.Year = model.LogsFilter.Year;
|
|
SessionData_Admin.System.Logs.Filter.Month = "";
|
|
//SessionData_Admin.System.Logs.Filter.Month = model.LogsFilter.Month;
|
|
|
|
// Identify folders in selected year folder.
|
|
String logFolder = Path.Combine(model.LoggingRootFolder, model.LogsFilter.Folder, model.LogsFilter.Year);
|
|
foreach (String folder in Directory.EnumerateDirectories(logFolder))
|
|
{
|
|
String folderName = Path.GetFileName(folder);
|
|
if (folderName.IsMatch("^\\d{6}$"))
|
|
responseData.Months.Add(new LogFolderData() { Name = folderName });
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Log File List ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_LogFileList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_LogFileList(SaxModel_Admin model) : base(model) { }
|
|
// Log file list.
|
|
public List<LogFileData> Files { get; } = new List<LogFileData>();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("System/Ajax/Logs/GetFileList")]
|
|
[Route("System/Ajax/Logs/GetFiles")]
|
|
public IActionResult Ajax_GetLogFileList(Model_System model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetLogFileList";
|
|
String messageTitle = Localiser["system.caption.logs"];
|
|
AjaxResponse_LogFileList responseData = new AjaxResponse_LogFileList(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Store filter properties to session.
|
|
SessionData_Admin.System.Logs.Filter.Folder = model.LogsFilter.Folder;
|
|
SessionData_Admin.System.Logs.Filter.Year = model.LogsFilter.Year;
|
|
SessionData_Admin.System.Logs.Filter.Month = model.LogsFilter.Month;
|
|
|
|
// Identify folders in selected year folder.
|
|
String logFolder = Path.Combine(model.LoggingRootFolder, model.LogsFilter.Folder, model.LogsFilter.Year, model.LogsFilter.Month);
|
|
DirectoryInfo folder = new DirectoryInfo(logFolder);
|
|
foreach (FileInfo file in folder.EnumerateFiles())
|
|
{
|
|
responseData.Files.Add(new LogFileData() { Name = file.Name, UpdateFmt = file.LastWriteTime.ToString("d-MMM-yy HH:mm"), Size = file.Length });
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Log File ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_LogFile : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_LogFile(SaxModel_Admin model) : base(model) { }
|
|
// Log file.
|
|
public class LogFileData
|
|
{
|
|
public String Path { get; set; }
|
|
public String Name { get; set; }
|
|
public String Data { get; set; }
|
|
public String Html { get; set; } = "";
|
|
}
|
|
public LogFileData File { get; } = new LogFileData();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("System/Ajax/Logs/GetFile")]
|
|
[Route("System/Ajax/Logs/GetLogFile")]
|
|
public IActionResult Ajax_GetLogFile(Model_System model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetLogFile";
|
|
String messageTitle = Localiser["system.caption.logs"];
|
|
AjaxResponse_LogFile responseData = new AjaxResponse_LogFile(model);
|
|
|
|
// Validate session.
|
|
if (!ValidUserSession)
|
|
return GetAjaxResult_InvalidSession(responseData);
|
|
|
|
// Store filter properties to session.
|
|
SessionData_Admin.System.Logs.Filter.Folder = model.LogsFilter.Folder;
|
|
SessionData_Admin.System.Logs.Filter.Year = model.LogsFilter.Year;
|
|
SessionData_Admin.System.Logs.Filter.Month = model.LogsFilter.Month;
|
|
|
|
// Identify folders in selected year folder.
|
|
responseData.File.Path = Path.Combine(model.LoggingRootFolder, model.LogsFilter.Folder, model.LogsFilter.Year, model.LogsFilter.Month, model.LogsFilter.Filename);
|
|
responseData.File.Name = Path.GetFileName(responseData.File.Path);
|
|
try
|
|
{
|
|
String[] fileLines = FileUtil.ReadAllFileLines(responseData.File.Path);
|
|
responseData.File.Data = String.Join("\n", fileLines);
|
|
// Construct html data.
|
|
foreach (var fileLine in fileLines)
|
|
{
|
|
String line = fileLine;
|
|
List<String> classNames = new List<string>();
|
|
if (fileLine.IsMatch("^\t{2}"))
|
|
{
|
|
line = Regex.Replace(fileLine, "^\t+", "");
|
|
classNames.Add("tab2");
|
|
}
|
|
if (fileLine.IsMatch("^\t"))
|
|
{
|
|
line = Regex.Replace(fileLine, "^\t", "");
|
|
classNames.Add("tab");
|
|
}
|
|
if (line.IsMatch("\\d{8}:\\d{6}\\s+(error|err)\\s+", RegexOptions.IgnoreCase))
|
|
classNames.Add("err");
|
|
else if (line.IsMatch("\\d{8}:\\d{6}\\s+(warn|wrn)\\s+", RegexOptions.IgnoreCase))
|
|
classNames.Add("wrn");
|
|
else if (line.IsMatch("\\d{8}:\\d{6}\\s+(info|inf)\\s+", RegexOptions.IgnoreCase))
|
|
classNames.Add("inf");
|
|
responseData.File.Html += $"<div class='{String.Join(" ", classNames)}'>" + line + "</div>";
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} {ExceptionUtil.GetLogMessage(exc, "reading log file", 0)}");
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|