Seriloog / SeriLoog / Controllers / LogsController.cs
Code · 42 lines · 1098 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42using Microsoft.AspNetCore.Mvc;
using SeriLoog.Services;

namespace SeriLoog.Controllers;

[ApiController]
[Route("api/[controller]")]
public sealed class LogsController(LogQueryService queryService) : ControllerBase
{
	[HttpGet]
	public async Task<IActionResult> Get(
		[FromQuery] int page = 1,
		[FromQuery] int pageSize = 25,
		[FromQuery] string? level = null,
		[FromQuery] string? search = null,
		[FromQuery] DateTimeOffset? from = null,
		[FromQuery] DateTimeOffset? to = null,
		[FromQuery] string source = "mongo",
		CancellationToken ct = default)
	{
		var levels = string.IsNullOrWhiteSpace(level)
			? null
			: level.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

		var query = new LogQuery
		{
			Page = page,
			PageSize = pageSize,
			Levels = levels,
			Search = search,
			From = from,
			To = to,
			Source = source
		};

		var result = await queryService.QueryAsync(query, ct);
		return Ok(result);
	}

	[HttpGet("levels")]
	public IActionResult Levels() => Ok(new[] { "Verbose", "Debug", "Information", "Warning", "Error", "Fatal" });
}