mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-30 16:58:38 +00:00
Add detailed exceptions to AspNetToKettuLogger
This commit is contained in:
parent
e5fed738db
commit
a6e489fb99
2 changed files with 27 additions and 3 deletions
22
ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs
Normal file
22
ProjectLighthouse/Helpers/Extensions/ExceptionExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue