using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Serilog; using Newtonsoft.Json; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Api.Internal.MaidBotLib.Repositories; using LCU.Library.Internal.Exceptions; namespace LCU.Api.Internal.MaidBotApi.Services; public class EventLogService : IEventLogService { private readonly IEventLogRepository _eventLogRepository; private readonly IDebugEventLogService _debugEventLogService; private readonly ILoggingMatrixService _loggingMatrixService; private readonly IUnitOfWork _unitOfWork; public EventLogService(IEventLogRepository eventLogRepository, IDebugEventLogService debugEventLogService, ILoggingMatrixService loggingMatrixService, IUnitOfWork unitOfWork) { _eventLogRepository = eventLogRepository; _debugEventLogService = debugEventLogService; _loggingMatrixService = loggingMatrixService; _unitOfWork = unitOfWork; } public async Task HandleIncomingEventLog(EventLog eventLog) { var loggingMatrixes = await _loggingMatrixService.GetByApplicationLevelAndGroupAsync(eventLog.Application, eventLog.Level, eventLog.Group); if (loggingMatrixes.Any()) { await SaveAsync(eventLog); } await _debugEventLogService.DebugEventLog(eventLog); } public async Task> GetAsync() { try { return await _eventLogRepository.ListAsync(); } catch (Exception exception) { var message = "Failure retrieving EventLogs."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}. "); } } public async Task SaveAsync(EventLog eventLog) { try { var result = await _eventLogRepository.AddAsync(eventLog); await _unitOfWork.CompleteAsync(); return result; } catch (Exception exception) { var message = $"Failure saving EventLog. EventLog: { JsonConvert.SerializeObject(eventLog)}"; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}."); } } public void DeleteMany(List eventLogs) { try { _eventLogRepository.RemoveRange(eventLogs); _unitOfWork.Complete(); } catch (Exception exception) { var message = $"Failure to delete EventLogs."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}."); } } }