Logging refactor

This commit is contained in:
Michał Zieliński
2025-06-02 18:53:25 +02:00
parent b800890320
commit ab3310a0c2
25 changed files with 682 additions and 750 deletions

View File

@@ -2,8 +2,8 @@ using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using DiunaBI.Database.Context;
using Google.Cloud.Firestore;
using DiunaBI.Core.Models;
namespace DiunaBI.WebAPI.Controllers;
@@ -14,16 +14,16 @@ public class DataInboxController : Controller
{
private readonly AppDbContext _db;
private readonly IConfiguration _configuration;
private readonly LogsController _logsController;
private readonly ILogger<DataInboxController> _logger;
public DataInboxController(
AppDbContext db,
IConfiguration configuration,
FirestoreDb firestoreDb)
ILogger<DataInboxController> logger)
{
_db = db;
_configuration = configuration;
_logsController = new LogsController(firestoreDb);
_logger = logger;
}
[HttpPut]
@@ -33,41 +33,22 @@ public class DataInboxController : Controller
{
if (apiKey != _configuration["apiKey"])
{
_logsController.AddEntry(new LogEntry
{
Title = $"Unauthorized request - wrong apiKey ({dataInbox.Source})",
Type = LogEntryType.Warning,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogWarning("DataInbox: Unauthorized request - wrong apiKey for source {Source}", dataInbox.Source);
return Unauthorized();
}
try
{
if (
!Request.Headers.TryGetValue("Authorization", out var authHeader))
if (!Request.Headers.TryGetValue("Authorization", out var authHeader))
{
_logsController.AddEntry(new LogEntry
{
Title = $"Unauthorized request - no authorization header ({dataInbox.Source})",
Type = LogEntryType.Warning,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogWarning("DataInbox: Unauthorized request - no authorization header for source {Source}", dataInbox.Source);
return Unauthorized();
}
var credentialsArr = authHeader.ToString().Split(" ");
if (credentialsArr.Length != 2)
{
_logsController.AddEntry(new LogEntry
{
Title = $"Unauthorized request - wrong auth header format ({dataInbox.Source})",
Type = LogEntryType.Warning,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogWarning("DataInbox: Unauthorized request - wrong auth header format for source {Source}", dataInbox.Source);
return Unauthorized();
}
@@ -76,60 +57,35 @@ public class DataInboxController : Controller
var password = authValue.Split(':')[1];
if (username != _configuration["morska-user"] || password != _configuration["morska-pass"])
{
_logsController.AddEntry(new LogEntry
{
Title = $"Unauthorized request - bad credentials ({dataInbox.Source})",
Type = LogEntryType.Warning,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogWarning("DataInbox: Unauthorized request - bad credentials for source {Source}", dataInbox.Source);
return Unauthorized();
}
// check if datainbox.data is base64 encoded value
if (!string.IsNullOrEmpty(dataInbox.Data) && !IsBase64String(dataInbox.Data))
{
_logsController.AddEntry(new LogEntry
{
Title = $"Invalid data format - not base64 encoded ({dataInbox.Source})",
Type = LogEntryType.Warning,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogWarning("DataInbox: Invalid data format - not base64 encoded for source {Source}", dataInbox.Source);
return BadRequest("Invalid data format - not base64 encoded");
}
dataInbox.Id = Guid.NewGuid();
dataInbox.CreatedAt = DateTime.UtcNow;
_db.DataInbox.Add(dataInbox);
_db.SaveChanges();
_logsController.AddEntry(new LogEntry
{
Title = $"Insert success: {dataInbox.Source}, {dataInbox.Name}",
Type = LogEntryType.Info,
LogType = LogType.DataInbox,
CreatedAt = DateTime.UtcNow
});
_logger.LogInformation("DataInbox: Insert success for source {Source}, name {Name}", dataInbox.Source, dataInbox.Name);
if (dataInbox.Name == "morska.d3.importer")
{
// TODO: import dataInbox as Layer
_logger.LogDebug("DataInbox: Detected morska.d3.importer - processing will be handled by AutoImport");
// AutoImport będzie obsługiwać ten typ danych
}
return Ok();
}
catch (Exception e)
{
_logsController.AddEntry(new LogEntry
{
Title = $"Insert error: {dataInbox.Source}, {dataInbox.Name}",
Type = LogEntryType.Error,
LogType = LogType.DataInbox,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
_logger.LogError(e, "DataInbox: Insert error for source {Source}, name {Name}", dataInbox.Source, dataInbox.Name);
return BadRequest(e.ToString());
}
}
@@ -137,7 +93,17 @@ public class DataInboxController : Controller
[HttpGet]
public IActionResult GetAll()
{
return Ok(_db.DataInbox.AsNoTracking().ToList());
try
{
var dataInbox = _db.DataInbox.AsNoTracking().ToList();
_logger.LogDebug("DataInbox: Retrieved {Count} records", dataInbox.Count);
return Ok(dataInbox);
}
catch (Exception e)
{
_logger.LogError(e, "DataInbox: Error retrieving records");
return BadRequest(e.ToString());
}
}
// helpers
@@ -150,9 +116,7 @@ public class DataInboxController : Controller
try
{
var base64Bytes = Convert.FromBase64String(data);
var utf8String = Encoding.UTF8.GetString(base64Bytes);
var reEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(utf8String));
return data.TrimEnd('=') == reEncoded.TrimEnd('=');
}