This commit is contained in:
2026-01-08 15:18:34 +01:00
parent d385119ca2
commit 33a0b77bf1
111 changed files with 85489 additions and 24 deletions

View File

@@ -0,0 +1,21 @@
namespace LogCentral.Server.Controllers;
public class LogEntryDto
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime Timestamp { get; set; }
public string ApplicationName { get; set; } = default!;
public string ApplicationVersion { get; set; } = default!;
public string Environment { get; set; } = default!;
public string MachineName { get; set; } = default!;
public int Level { get; set; }
public string Category { get; set; } = default!;
public string Message { get; set; } = default!;
public string? Exception { get; set; }
public string? StackTrace { get; set; }
public Dictionary<string, object>? Properties { get; set; }
public string? UserId { get; set; }
public string? SessionId { get; set; }
public string? RequestId { get; set; }
public string? CorrelationId { get; set; }
}

View File

@@ -0,0 +1,74 @@
using Microsoft.AspNetCore.Mvc;
using LogCentral.Server.Services;
using LogCentral.Server.Models;
namespace LogCentral.Server.Controllers;
[ApiController]
[Route("api/[controller]")]
public class LogsController : ControllerBase
{
private readonly ILogService _logService;
public LogsController(ILogService logService)
{
_logService = logService;
}
[HttpPost("batch")]
public async Task<IActionResult> PostBatch([FromBody] List<LogEntryDto> entries)
{
var apiKey = Request.Headers["X-API-Key"].FirstOrDefault();
if (string.IsNullOrEmpty(apiKey) || !await _logService.ValidateApiKeyAsync(apiKey))
{
return Unauthorized(new { error = "Invalid API key" });
}
// Map DTO -> EF entity
var logEntities = entries.Select(dto => new LogEntry
{
Id = dto.Id,
Timestamp = dto.Timestamp,
ApplicationName = dto.ApplicationName,
ApplicationVersion = dto.ApplicationVersion,
Environment = dto.Environment,
MachineName = dto.MachineName,
Level = dto.Level,
Category = dto.Category,
Message = dto.Message,
Exception = dto.Exception,
StackTrace = dto.StackTrace,
Properties = dto.Properties,
UserId = dto.UserId,
SessionId = dto.SessionId,
RequestId = dto.RequestId,
CorrelationId = dto.CorrelationId
}).ToList();
await _logService.AddLogsAsync(logEntities);
return Ok(new { success = true, count = entries.Count });
}
[HttpGet]
public async Task<IActionResult> GetLogs([FromQuery] LogQueryParams queryParams)
{
var result = await _logService.GetLogsAsync(queryParams);
return Ok(result);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetLog(string id)
{
var log = await _logService.GetLogByIdAsync(id);
if (log == null) return NotFound();
return Ok(log);
}
[HttpGet("stats")]
public async Task<IActionResult> GetStats([FromQuery] string? app, [FromQuery] string? env)
{
var stats = await _logService.GetStatsAsync(app, env);
return Ok(stats);
}
}