using Serilog; using System; using System.Linq; using System.Threading.Tasks; namespace LCU.Api.Internal.MaidBotApi.Services; public class LogDistributionService : ILogDistributionService { private readonly IEventLogService _eventLogService; private readonly ILoggingMatrixService _loggingMatrixService; private readonly INotificationsApiService _notificationsApiService; public LogDistributionService(IEventLogService eventLogService, ILoggingMatrixService loggingMatrixService, INotificationsApiService notificationsApiService) { _eventLogService = eventLogService; _loggingMatrixService = loggingMatrixService; _notificationsApiService = notificationsApiService; } public async Task HandleLogDistribution() { try { var eventLogs = await _eventLogService.GetAsync(); if(eventLogs == null || !eventLogs.Any()) { return; } var loggingMatrixs = await _loggingMatrixService.GetActiveAsync(); if (loggingMatrixs == null || !loggingMatrixs.Any()) { return; } foreach (var loggingMatrix in loggingMatrixs) { var matrixEventLogs = eventLogs.Where(eventLog => eventLog.Application == loggingMatrix.Application && eventLog.Level == loggingMatrix.Level && eventLog.Group == loggingMatrix.Group).ToList(); if (!matrixEventLogs.Any()) { continue; } await _notificationsApiService.SendNotificationAsync(loggingMatrix, matrixEventLogs); } _eventLogService.DeleteMany(eventLogs.ToList()); } catch (Exception exception) { Log.Error(exception, "Failure to Handle Log Distribution."); } } }