Add detailed exceptions to AspNetToKettuLogger

This commit is contained in:
jvyden 2021-10-22 23:10:32 -04:00
commit a6e489fb99
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 27 additions and 3 deletions

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace LBPUnion.ProjectLighthouse.Helpers.Extensions {
// https://stackoverflow.com/a/8039737
public static class ExceptionExtensions {
public static string ToDetailedException(this Exception exception) {
PropertyInfo[] properties = exception.GetType().GetProperties();
IEnumerable<string> fields = properties
.Select(property => new {
property.Name,
Value = property.GetValue(exception, null)
})
.Select(x => $"{x.Name} = {(x.Value != null ? x.Value.ToString() : string.Empty)}");
return string.Join("\n", fields);
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using Microsoft.Extensions.Logging;
namespace LBPUnion.ProjectLighthouse.Logging {
@ -24,9 +25,10 @@ namespace LBPUnion.ProjectLighthouse.Logging {
};
Logger.Log(state.ToString(), loggerLevel);
if(exception != null) {
Logger.Log(exception.ToString(), loggerLevel);
}
if(exception == null) return;
string[] lines = exception.ToDetailedException().Replace("\r", "").Split("\n");
foreach(string line in lines) Logger.Log(line, loggerLevel);
}
}
}