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