diff --git a/Moonlight/App/Services/LogServices/ErrorLogService.cs b/Moonlight/App/Services/LogServices/ErrorLogService.cs index 6c419450..4328c95a 100644 --- a/Moonlight/App/Services/LogServices/ErrorLogService.cs +++ b/Moonlight/App/Services/LogServices/ErrorLogService.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Reflection; using Moonlight.App.Database.Entities.LogsEntries; +using Moonlight.App.Models.Log; using Moonlight.App.Repositories.LogEntries; using Moonlight.App.Services.Sessions; using Newtonsoft.Json; @@ -18,15 +19,17 @@ public class ErrorLogService HttpContextAccessor = httpContextAccessor; } - public Task Log(Exception exception, params object[] objects) + public Task Log(Exception exception, Action data) { var ip = GetIp(); + var al = new ErrorLogParameters(); + data(al); var entry = new ErrorLogEntry() { Ip = ip, System = false, - JsonData = !objects.Any() ? "" : JsonConvert.SerializeObject(objects), + JsonData = al.Build(), Class = NameOfCallingClass(), Stacktrace = exception.ToStringDemystified() }; @@ -36,12 +39,15 @@ public class ErrorLogService return Task.CompletedTask; } - public Task LogSystem(Exception exception, params object[] objects) + public Task LogSystem(Exception exception, Action data) { + var al = new ErrorLogParameters(); + data(al); + var entry = new ErrorLogEntry() { System = true, - JsonData = !objects.Any() ? "" : JsonConvert.SerializeObject(objects), + JsonData = al.Build(), Class = NameOfCallingClass(), Stacktrace = exception.ToStringDemystified() }; @@ -87,4 +93,23 @@ public class ErrorLogService return HttpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString(); } + + public class ErrorLogParameters + { + private List Data = new List(); + + public void Add(object data) + { + Data.Add(new LogData() + { + Type = typeof(T), + Value = data.ToString() + }); + } + + internal string Build() + { + return JsonConvert.SerializeObject(Data); + } + } } \ No newline at end of file diff --git a/Moonlight/App/Services/ServerService.cs b/Moonlight/App/Services/ServerService.cs index 84522e8e..71c37784 100644 --- a/Moonlight/App/Services/ServerService.cs +++ b/Moonlight/App/Services/ServerService.cs @@ -334,7 +334,11 @@ public class ServerService } catch (Exception e) { - await ErrorLogService.Log(e, new[] { newServerData.Uuid.ToString(), node.Id.ToString() }); + await ErrorLogService.Log(e, x => + { + x.Add(newServerData.Uuid); + x.Add(node.Id); + }); ServerRepository.Delete(newServerData); diff --git a/Moonlight/App/Services/Sessions/IdentityService.cs b/Moonlight/App/Services/Sessions/IdentityService.cs index bce93828..eaeac7b1 100644 --- a/Moonlight/App/Services/Sessions/IdentityService.cs +++ b/Moonlight/App/Services/Sessions/IdentityService.cs @@ -94,7 +94,7 @@ public class IdentityService } catch (Exception e) { - await ErrorLogService.Log(e); + await ErrorLogService.Log(e, x => {}); return null; } @@ -130,7 +130,7 @@ public class IdentityService } catch (Exception e) { - await ErrorLogService.Log(e); + await ErrorLogService.Log(e, x => {}); return null; } } diff --git a/Moonlight/Shared/Components/ErrorBoundaries/ComponentErrorBoundary.razor b/Moonlight/Shared/Components/ErrorBoundaries/ComponentErrorBoundary.razor index d8f29c90..c540c4c5 100644 --- a/Moonlight/Shared/Components/ErrorBoundaries/ComponentErrorBoundary.razor +++ b/Moonlight/Shared/Components/ErrorBoundaries/ComponentErrorBoundary.razor @@ -53,7 +53,7 @@ else { receivedExceptions.Add(exception); - await ErrorLogService.Log(exception); + await ErrorLogService.Log(exception, x => {}); await base.OnErrorAsync(exception); }