Ensure the logger doesn't fail to log based on some stacktrace field during json serialization
This commit is contained in:
parent
e13cf9d830
commit
c2c30846fb
3 changed files with 38 additions and 13 deletions
19
src/Core/Logger/ExceptionDto.cs
Normal file
19
src/Core/Logger/ExceptionDto.cs
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
|
@ -41,7 +41,7 @@ namespace Geekbot.Core.Logger
|
||||||
|
|
||||||
public bool LogAsJson() => _logAsJson;
|
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)
|
if (_logAsJson)
|
||||||
{
|
{
|
||||||
|
@ -51,7 +51,7 @@ namespace Geekbot.Core.Logger
|
||||||
Type = type,
|
Type = type,
|
||||||
Source = source,
|
Source = source,
|
||||||
Message = message,
|
Message = message,
|
||||||
StackTrace = stackTrace,
|
StackTrace = exception != null ? new ExceptionDto(exception) : null,
|
||||||
Extra = extra
|
Extra = extra
|
||||||
};
|
};
|
||||||
return JsonSerializer.Serialize(logObject, _serializerSettings);
|
return JsonSerializer.Serialize(logObject, _serializerSettings);
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
using System;
|
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 DateTime Timestamp { get; set; }
|
public string Type { get; set; }
|
||||||
public string Type { get; set; }
|
|
||||||
public LogSource Source { get; set; }
|
public LogSource Source { get; set; }
|
||||||
public string Message { get; set; }
|
|
||||||
public Exception StackTrace { get; set; }
|
public string Message { get; set; }
|
||||||
public object Extra { get; set; }
|
|
||||||
}
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
|
public ExceptionDto? StackTrace { get; set; }
|
||||||
|
|
||||||
|
public object Extra { get; set; }
|
||||||
}
|
}
|
Loading…
Reference in a new issue