Files
EonaCat.LogStack/EonaCat.LogStack.Status/Pages/Logs.cshtml.cs
EonaCat 977374ce02 Added EonaCat.LogStack.Status
Updated EonaCat.LogStack.LogClient to support EonaCat.LogStack.Status
2026-03-12 21:15:33 +01:00

67 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using EonaCat.LogStack.Status.Data;
using EonaCat.LogStack.Status.Models;
namespace EonaCat.LogStack.Status.Pages;
public class LogsModel : PageModel
{
private readonly DatabaseContext _db;
public LogsModel(DatabaseContext db) => _db = db;
public List<LogEntry> Entries { get; set; } = new();
public List<string> Sources { get; set; } = new();
public int TotalCount { get; set; }
public int TotalPages { get; set; }
[BindProperty(SupportsGet = true)]
public string? Level { get; set; }
[BindProperty(SupportsGet = true)]
public string? Source { get; set; }
[BindProperty(SupportsGet = true)]
public string? Search { get; set; }
[BindProperty(SupportsGet = true)]
public int PageIndex { get; set; } = 1;
public async Task<IActionResult> OnGetAsync()
{
if (HttpContext.Session.GetString("IsAdmin") != "true")
{
return RedirectToPage("/Admin/Login");
}
Sources = await _db.Logs.Select(l => l.Source).Distinct().OrderBy(s => s).ToListAsync();
var q = _db.Logs.AsQueryable();
if (!string.IsNullOrWhiteSpace(Level))
{
q = q.Where(l => l.Level == Level.ToLower());
}
if (!string.IsNullOrWhiteSpace(Source))
{
q = q.Where(l => l.Source == Source);
}
if (!string.IsNullOrWhiteSpace(Search))
{
q = q.Where(l => l.Message.Contains(Search) || (l.Exception != null && l.Exception.Contains(Search)));
}
TotalCount = await q.CountAsync();
TotalPages = (int)Math.Ceiling((double)TotalCount / 100);
Entries = await q.OrderByDescending(l => l.Timestamp)
.Skip((PageIndex - 1) * 100)
.Take(100)
.ToListAsync();
return Page();
}
}