public void ShouldWrite() { var inner1Ex = new NullReferenceException("inner1Message"); var inner2Ex = new InvalidOperationException("inner2Message", inner1Ex); var outerEx = new Exception("outerMessage", inner2Ex); var expectedOutput = @"---------- outerMessage ---------- Debug information: ---------- Exception: outerMessage [StackTrace:Exception] ---------- inner InvalidOperationException: inner2Message [StackTrace:InvalidOperationException] ---------- inner NullReferenceException: inner1Message [StackTrace:NullReferenceException] ---------- "; Func<Exception, string> stackTraceFormatter = ex => string.Format("[StackTrace:{0}]", ex.GetType().Name); var actual = outerEx.ToLogString(stackTraceFormatter); Assert.That(expectedOutput, Is.EqualTo(actual)); }
public static IDbUpgradeScript NewDbModelChangeScript(this IDbUpgradeBatch batch, DbObjectChangeType changeType, DbObjectType objectType, string fullObjectName, string sql, int executionOrder, int subOrder, int duration, Exception exception) { var session = EntityHelper.GetSession(batch); var ent = session.NewEntity<IDbUpgradeScript>(); ent.Batch = batch; ent.ObjectType = objectType; ent.FullObjectName = fullObjectName; ent.ExecutionOrder = executionOrder; ent.SubOrder = subOrder; ent.Sql = sql; ent.Duration = duration; if(exception != null) ent.Errors = exception.ToLogString(); return ent; }
public static IDbUpgradeBatch NewDbModelChangeBatch(this IEntitySession session, string fromVersion, string toVersion, DateTime startedOn, DateTime? completedOn, DbUpgradeMethod method, string machineName, string userName, Exception exception = null) { var ent = session.NewEntity<IDbUpgradeBatch>(); ent.FromVersion = fromVersion; ent.ToVersion = toVersion; ent.StartedOn = startedOn; ent.CompletedOn = completedOn; ent.Method = method; ent.MachineName = machineName ?? Environment.MachineName; ent.UserName = userName ?? Environment.UserName; if (exception == null) ent.Success = true; else { ent.Success = false; ent.Errors = exception.ToLogString(); } return ent; }
public static IDbUpgradeScript NewDbModelChangeScript(this IDbUpgradeBatch batch, DbUpgradeScript script, int index = 0, Exception exception = null) { var session = EntityHelper.GetSession(batch); var ent = session.NewEntity<IDbUpgradeScript>(); ent.Batch = batch; if (script.Migration != null) { ent.ObjectType = DbObjectType.Other; ent.FullObjectName = script.Migration.Name; } else if (script.ModelChange != null) { ent.ObjectType = script.ModelChange.ObjectType; ent.FullObjectName = script.ModelChange.DbObject.GlobalName; } else { ent.ObjectType = DbObjectType.Other; ent.FullObjectName = "(Unknown)"; } ent.ExecutionOrder = index; ent.Sql = script.Sql; ent.Duration = script.Duration; if(exception != null) ent.Errors = exception.ToLogString(); return ent; }
public Guid LogError(Exception exception, OperationContext context = null) { if(!this.App.IsConnected()) { OnErrorLogged(context, exception); Util.WriteToTrace(exception, context.GetLogContents(), copyToEventLog: true); return Guid.Empty; } try { var session = this.App.OpenSystemSession(); session.DisableStoredProcs(); //as a precaution, taking simpler path, in case something wrong with stored procs var errInfo = session.NewEntity<IErrorLog>(); errInfo.CreatedOn = App.TimeService.UtcNow; errInfo.LocalTime = App.TimeService.Now; errInfo.Message = Util.CheckLength(exception.Message, 250); errInfo.Details = exception.ToLogString(); //writes exc.ToString() and exc.Data collection, along with all inner exception details errInfo.ExceptionType = exception.GetType().Name; errInfo.MachineName = Environment.MachineName; if(context != null) { errInfo.AppName = context.App.AppName; errInfo.OperationLog = context.GetLogContents(); errInfo.UserName = context.User.UserName; if(context.UserSession != null) errInfo.UserSessionId = context.UserSession.SessionId; if (context.WebContext != null) errInfo.WebCallId = context.WebContext.Id; } session.SaveChanges(); OnErrorLogged(context, exception); return errInfo.Id; } catch (Exception logEx) { Util.WriteToTrace(logEx, "Fatal failure in database error log. See next error log entry for original error."); Util.WriteToTrace(exception, null, copyToEventLog: true); return Guid.NewGuid(); } }
/// <summary> /// Sends this error as a notification email to the address in web.config as Error.Notification.Receiver. /// </summary> public static IEmailQueueItem SendAsNotification(this Exception error, string toNotify) { if (toNotify.IsEmpty()) { return(null); } var email = EmailService.EmailQueueItemFactory(); email.To = toNotify; email.Subject = "Error In Application"; email.Body = "URL: " + HttpContext.Current?.Request?.Url + Environment.NewLine + "IP: " + HttpContext.Current?.Request?.UserHostAddress + Environment.NewLine + "User: " + ApplicationEventManager.GetCurrentUserId(HttpContext.Current?.User) + Environment.NewLine + error.ToLogString(error.Message); Database.Save(email); return(email); }
private static void WriteException(Exception ex) { var err = ex.ToLogString(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); Console.WriteLine("Exception: "); Console.WriteLine(err); Console.ResetColor(); LogError(err + "\r\n"); }
public bool UpdateDbInfo(Database db, Exception exception = null) { //Check that db has module's tables; if not, this module is not included in the solution var tbl = db.DbModel.GetTable(typeof(IDbInfo)); if (tbl == null) return false; try { var app = db.DbModel.EntityApp; var session = App.OpenSystemSession(); // Disable stored procs and disable batch mode session.DisableStoredProcs(); session.DisableBatchMode(); var ent = session.GetEntities<IDbInfo>(take: 1).FirstOrDefault(e => e.AppName == app.AppName); if(ent == null) { ent = session.NewEntity<IDbInfo>(); ent.Version = app.Version.ToString(); ent.AppName = app.AppName; } if(exception == null) { ent.Version = app.Version.ToString(); ent.LastModelUpdateFailed = false; ent.LastModelUpdateError = null; ent.Values = SerializeValues(db.DbModel.VersionInfo); SaveModulesInfo(session, db.DbModel.VersionInfo); } else { ent.LastModelUpdateFailed = true; ent.LastModelUpdateError = exception.ToLogString(); } // we use db.SaveChanges directly, to make sure we go thru proper database var entSession = (Vita.Entities.Runtime.EntitySession)session; db.SaveChanges(entSession); return true; } catch (Exception ex) { App.ActivationLog.Error(ex.ToLogString()); return false; } }
private static void WriteInnerException(StringBuilder sb, Exception inner) { if (inner == null) return; sb.AppendLine(" InnerException: ------------------------------------------------"); sb.AppendLine(inner.ToLogString()); sb.AppendLine(" EndInnerException: ---------------------------------------------"); }