From a6e489fb998ff8e0d49b254f5e260b3ce33272c2 Mon Sep 17 00:00:00 2001 From: jvyden Date: Fri, 22 Oct 2021 23:10:32 -0400 Subject: [PATCH] Add detailed exceptions to AspNetToKettuLogger --- .../Helpers/Extensions/ExceptionExtensions.cs | 22 +++++++++++++++++++ .../Logging/AspNetToKettuLogger.cs | 8 ++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs diff --git a/ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs b/ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs new file mode 100644 index 00000000..aee5e0ce --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs @@ -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 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); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/AspNetToKettuLogger.cs b/ProjectLighthouse/Logging/AspNetToKettuLogger.cs index 8dd7d38e..944fafa5 100644 --- a/ProjectLighthouse/Logging/AspNetToKettuLogger.cs +++ b/ProjectLighthouse/Logging/AspNetToKettuLogger.cs @@ -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); } } } \ No newline at end of file