private static void PreProcess(Severity severity, string additionalMessage, Exception exception) { var callStack = new StackTrace(1, true); var stackFrame = callStack.GetFrame(1); var log = new Log { MethodName = stackFrame.GetMethod().Name, ClassName = stackFrame.GetMethod().DeclaringType.Name, ApplicationName = stackFrame.GetMethod().DeclaringType.Assembly.GetName().Name, LineNumber = stackFrame.GetFileLineNumber(), Severity = (int)severity, Message = $"Message: {additionalMessage ?? ""}" }; if (exception.IsNotNull()) { log.Message += $" Exception: {(exception.IsNotNull() ? exception.ToString() : "")}"; } using (var ctx = new ApplicationDbContext()) { ctx.Logs.Add(log); ctx.SaveChanges(); } }
/// <summary> /// Formats an exception to be placed in the debug output. /// </summary> /// <param name="ex"> /// The exception. /// </param> /// <returns> /// A string that represents the message to display for the exception. /// </returns> protected string FormatException(Exception ex) { var builder = new StringBuilder(); if (ex.IsNotNull()) { builder.AppendLine(ex.Message); builder.AppendLine(ex.StackTrace); var aggex = ex as AggregateException; if (aggex.IsNotNull()) { foreach (Exception innerException in aggex.InnerExceptions) { builder.AppendLine(this.FormatException(innerException)); } } else if (ex.InnerException.IsNotNull()) { builder.AppendLine(this.FormatException(ex.InnerException)); } } return builder.ToString(); }
private bool IsFatalException(Exception e) { var fatalTypes = new List<Type> { typeof(ArgumentException) }; bool rv = false; if (e.IsNotNull()) { Exception finalException = GetFirstException(e); Type exceptionType = finalException.GetType(); foreach (Type t in fatalTypes) { if (t.IsAssignableFrom(exceptionType)) { rv = true; break; } } } return rv; }