Ensure the logger doesn't fail to log based on some stacktrace field during json serialization

This commit is contained in:
Daan Boerlage 2021-11-10 00:43:09 +01:00
parent e13cf9d830
commit c2c30846fb
Signed by: daan
GPG key ID: FCE070E1E4956606
3 changed files with 38 additions and 13 deletions

View file

@ -0,0 +1,19 @@
using System;
namespace Geekbot.Core.Logger;
public struct ExceptionDto
{
public string Message { get; init; }
public string InnerException { get; init; }
public string Source { get; init; }
public ExceptionDto(Exception exception)
{
Message = exception.Message;
InnerException = string.IsNullOrEmpty(exception?.InnerException?.ToString()) ? exception?.StackTrace : exception?.InnerException?.ToString();
Source = exception.Source;
}
};

View file

@ -41,7 +41,7 @@ namespace Geekbot.Core.Logger
public bool LogAsJson() => _logAsJson;
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
private string CreateLogString(string type, LogSource source, string message, Exception exception = null, object extra = null)
{
if (_logAsJson)
{
@ -51,7 +51,7 @@ namespace Geekbot.Core.Logger
Type = type,
Source = source,
Message = message,
StackTrace = stackTrace,
StackTrace = exception != null ? new ExceptionDto(exception) : null,
Extra = extra
};
return JsonSerializer.Serialize(logObject, _serializerSettings);

View file

@ -1,14 +1,20 @@
using System;
using System.Text.Json.Serialization;
namespace Geekbot.Core.Logger
namespace Geekbot.Core.Logger;
public struct GeekbotLoggerObject
{
public class GeekbotLoggerObject
{
public DateTime Timestamp { get; set; }
public string Type { get; set; }
public LogSource Source { get; set; }
public string Message { get; set; }
public Exception StackTrace { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public ExceptionDto? StackTrace { get; set; }
public object Extra { get; set; }
}
}