error log

This commit is contained in:
Daniel Balk
2023-04-03 19:51:29 +02:00
parent b6ef64a766
commit 5b807a667d
4 changed files with 37 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using Moonlight.App.Database.Entities.LogsEntries; using Moonlight.App.Database.Entities.LogsEntries;
using Moonlight.App.Models.Log;
using Moonlight.App.Repositories.LogEntries; using Moonlight.App.Repositories.LogEntries;
using Moonlight.App.Services.Sessions; using Moonlight.App.Services.Sessions;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -18,15 +19,17 @@ public class ErrorLogService
HttpContextAccessor = httpContextAccessor; HttpContextAccessor = httpContextAccessor;
} }
public Task Log(Exception exception, params object[] objects) public Task Log(Exception exception, Action<ErrorLogParameters> data)
{ {
var ip = GetIp(); var ip = GetIp();
var al = new ErrorLogParameters();
data(al);
var entry = new ErrorLogEntry() var entry = new ErrorLogEntry()
{ {
Ip = ip, Ip = ip,
System = false, System = false,
JsonData = !objects.Any() ? "" : JsonConvert.SerializeObject(objects), JsonData = al.Build(),
Class = NameOfCallingClass(), Class = NameOfCallingClass(),
Stacktrace = exception.ToStringDemystified() Stacktrace = exception.ToStringDemystified()
}; };
@@ -36,12 +39,15 @@ public class ErrorLogService
return Task.CompletedTask; return Task.CompletedTask;
} }
public Task LogSystem(Exception exception, params object[] objects) public Task LogSystem(Exception exception, Action<ErrorLogParameters> data)
{ {
var al = new ErrorLogParameters();
data(al);
var entry = new ErrorLogEntry() var entry = new ErrorLogEntry()
{ {
System = true, System = true,
JsonData = !objects.Any() ? "" : JsonConvert.SerializeObject(objects), JsonData = al.Build(),
Class = NameOfCallingClass(), Class = NameOfCallingClass(),
Stacktrace = exception.ToStringDemystified() Stacktrace = exception.ToStringDemystified()
}; };
@@ -87,4 +93,23 @@ public class ErrorLogService
return HttpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString(); return HttpContextAccessor.HttpContext.Connection.RemoteIpAddress!.ToString();
} }
public class ErrorLogParameters
{
private List<LogData> Data = new List<LogData>();
public void Add<T>(object data)
{
Data.Add(new LogData()
{
Type = typeof(T),
Value = data.ToString()
});
}
internal string Build()
{
return JsonConvert.SerializeObject(Data);
}
}
} }

View File

@@ -334,7 +334,11 @@ public class ServerService
} }
catch (Exception e) catch (Exception e)
{ {
await ErrorLogService.Log(e, new[] { newServerData.Uuid.ToString(), node.Id.ToString() }); await ErrorLogService.Log(e, x =>
{
x.Add<Server>(newServerData.Uuid);
x.Add<Node>(node.Id);
});
ServerRepository.Delete(newServerData); ServerRepository.Delete(newServerData);

View File

@@ -94,7 +94,7 @@ public class IdentityService
} }
catch (Exception e) catch (Exception e)
{ {
await ErrorLogService.Log(e); await ErrorLogService.Log(e, x => {});
return null; return null;
} }
@@ -130,7 +130,7 @@ public class IdentityService
} }
catch (Exception e) catch (Exception e)
{ {
await ErrorLogService.Log(e); await ErrorLogService.Log(e, x => {});
return null; return null;
} }
} }

View File

@@ -53,7 +53,7 @@ else
{ {
receivedExceptions.Add(exception); receivedExceptions.Add(exception);
await ErrorLogService.Log(exception); await ErrorLogService.Log(exception, x => {});
await base.OnErrorAsync(exception); await base.OnErrorAsync(exception);
} }