include more inner exception data in system logging

This commit is contained in:
Gary Sharp
2023-11-08 16:51:08 +11:00
parent 83557e6f0c
commit 23406e5e39
2 changed files with 63 additions and 8 deletions
@@ -2,6 +2,8 @@
using Disco.Data.Repository.Monitor;
using Disco.Models.Repository;
using Disco.Models.Services.Jobs.JobLists;
using Disco.Services.Authorization;
using Disco.Services.Jobs.JobQueues;
using Disco.Services.Logging;
using Disco.Services.Users;
using System;
@@ -12,10 +14,8 @@ using System.Reactive.Linq;
namespace Disco.Services.Jobs.JobLists
{
using FilterFunc = Func<IQueryable<Job>, IQueryable<Job>>;
using SortFunc = Func<IEnumerable<JobTableItemModel>, IEnumerable<JobTableItemModel>>;
using OpenFilterFunc = Func<IEnumerable<JobTableStatusItemModel>, IEnumerable<JobTableStatusItemModel>>;
using Disco.Services.Authorization;
using Disco.Services.Jobs.JobQueues;
using SortFunc = Func<IEnumerable<JobTableItemModel>, IEnumerable<JobTableItemModel>>;
public class ManagedJobList : JobTableModel, IDisposable
{
+59 -4
View File
@@ -1,4 +1,5 @@
using Disco.Services.Logging.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
@@ -44,10 +45,8 @@ namespace Disco.Services.Logging
public static void LogException(string Component, Exception ex)
{
// Handle Special-Case Errors
if (ex is System.Data.Entity.Validation.DbEntityValidationException)
if (ex is System.Data.Entity.Validation.DbEntityValidationException dbException)
{
var dbException = (System.Data.Entity.Validation.DbEntityValidationException)ex;
StringBuilder message = new StringBuilder();
message.AppendLine("Validation failed for one or more entities:");
foreach (var dbEntityError in dbException.EntityValidationErrors)
@@ -64,7 +63,15 @@ namespace Disco.Services.Logging
{
if (ex.InnerException != null)
{
Log(EventTypeIds.ExceptionWithInner, Component, ex.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace);
// serialize inner exceptions for advanced troubleshooting
var serialized = string.Empty;
try
{
serialized = JsonConvert.SerializeObject(new SerializedException(ex, 5));
}
catch (Exception) { }
Log(EventTypeIds.ExceptionWithInner, Component, ex.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace, serialized);
}
else
{
@@ -72,6 +79,54 @@ namespace Disco.Services.Logging
}
}
}
private class SerializedException
{
public string Type { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public List<SerializedException> Children { get; set; }
public SerializedException(Exception ex, int depth)
{
Type = ex.GetType().Name;
StackTrace = ex.StackTrace;
Message = ex.Message;
if (ex is System.Data.Entity.Validation.DbEntityValidationException dbException)
{
StringBuilder message = new StringBuilder();
message.AppendLine("Validation failed for one or more entities:");
foreach (var dbEntityError in dbException.EntityValidationErrors)
{
message.Append("'").Append(dbEntityError.Entry.Entity.GetType().Name).AppendLine("' Object");
foreach (var dbValidationError in dbEntityError.ValidationErrors)
{
message.Append(" ").Append(dbValidationError.PropertyName).Append(": ").AppendLine(dbValidationError.ErrorMessage);
}
}
Message = message.ToString();
}
if (depth > 0)
{
if (ex is AggregateException exAg)
{
Children = new List<SerializedException>();
foreach (var inner in exAg.InnerExceptions)
{
Children.Add(new SerializedException(inner, depth - 1));
}
}
else if (ex.InnerException != null)
{
Children = new List<SerializedException>()
{
new SerializedException(ex.InnerException, depth - 1)
};
}
}
}
}
public static void LogLogInitialized(string PersistantStorePath)
{