/// <inheritdoc /> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { await ActionAsync(stoppingToken).ConfigureAwait(false); LogAndResetLastExceptionIfNotNull(); } catch (Exception ex) when(ex is OperationCanceledException || ex is TaskCanceledException || ex is TimeoutException) { Logger.LogTrace(ex); } catch (Exception ex) { // Only log one type of exception once. if (LastException is null || // If the exception never came. ex.GetType() != LastException.GetType() || // Or the exception have different type from previous exception. ex.Message != LastException.Message) // Or the exception have different message from previous exception. { // Then log and reset the last exception if another one came before. LogAndResetLastExceptionIfNotNull(); // Set new exception and log it. LastException = ex; LastExceptionFirstAppeared = DateTimeOffset.UtcNow; LastExceptionCount = 1; Logger.LogError(ex); }
/// <summary> /// Takes the parsed error information from the object properties and parses this information into a string, /// that can be displayed as plain text or in a browser. The string is formatted text and displays well /// in a browser using a PRE tag. /// /// Method also displays request information including: /// /// Full Url /// Refering Url /// IP Address of caller /// Client Browser /// Full POST buffer /// /// Full handling also returns: /// /// Stack Trace /// Source code blocks of the 5 lines before and after failure line if and/or debugging info is available /// <returns>Formatted Error string</returns> public override string ToString() { if (!IsParsed) { Parse(); } StringBuilder sb = new StringBuilder(); sb.AppendLine(ErrorMessage + "\r\n\r\n"); sb.AppendLine("--- Base Error Info ---"); sb.AppendLine("Exception: " + LastException.GetType().Name); sb.AppendFormat(" Url: {0}\r\n", RawUrl); sb.AppendFormat(" Time: {0}\r\n", DateTime.Now.ToString("MMM dd, yyyy HH:mm")); if (CompactFormat) { return(sb.ToString()); } if (!string.IsNullOrEmpty(SourceCode)) { sb.AppendLine("\r\n--- Code ---"); sb.Append(SourceCode); } if (!string.IsNullOrEmpty(StackTrace)) { sb.AppendFormat("\r\n--- Stack Trace ---\r\n{0}\r\n\r\n", StackTrace); } sb.Append("\r\n--- Request Information ---\r\n"); sb.AppendFormat(" Full Url: {0}\r\n", FullUrl); sb.AppendFormat(" Client IP: {0}\r\n", IPAddress); if (!string.IsNullOrEmpty(Referer)) { sb.AppendFormat(" Referer: {0}\r\n", Referer); } sb.AppendFormat(" Browser: {0}\r\n", Browser); sb.AppendFormat(" Login: {0}\r\n", Login); sb.AppendFormat(" Locale: {0}\r\n", Locale); sb.AppendFormat(" Status: {0}\r\n", OriginalHttpStatusCode); if (!string.IsNullOrEmpty(PostBuffer)) { sb.AppendFormat("\r\n\r\n--- Raw Post Buffer ---\r\n\r\n{0}", PostBuffer); } if (!string.IsNullOrEmpty(ServerVariables)) { sb.AppendFormat("\r\n\r\n--- All Server Variables ---\r\n\r\n{0}", ServerVariables); } return(sb.ToString()); }
protected override void OnInitialized() { ExceptionHandlerBuilder.Handler(e => { bool isSkipped = false; Type exceptionType = e.GetType(); foreach (Type type in SkippedExceptions) { if (type.IsAssignableFrom(exceptionType)) { isSkipped = true; break; } } if (!isSkipped) { LastException = e; if (e is Neptuo.Models.AggregateRootException) { string message = null; if (e is CurrencyAlreadyAsDefaultException) { message = MessageBuilder.CurrencyAlreadyAsDefault(); } else if (e is CurrencyAlreadyExistsException) { message = MessageBuilder.CurrencyAlreadyExists(); } else if (e is CurrencyDoesNotExistException) { message = MessageBuilder.CurrencyDoesNotExist(); } else if (e is CurrencyExchangeRateDoesNotExistException) { message = MessageBuilder.CurrencyExchangeRateDoesNotExist(); } else if (e is OutcomeAlreadyDeletedException) { message = MessageBuilder.OutcomeAlreadyDeleted(); } else if (e is OutcomeAlreadyHasCategoryException) { message = MessageBuilder.OutcomeAlreadyHasCategory(); } else if (e is CantDeleteDefaultCurrencyException) { message = MessageBuilder.CantDeleteDefaultCurrency(); } else if (e is CantDeleteLastCurrencyException) { message = MessageBuilder.CantDeleteLastCurrency(); } else if (e is DemoUserCantBeChangedException) { message = MessageBuilder.DemoUserCantBeChanged(); } else if (e is PasswordChangeFailedException passwordChangeFailed) { message = MessageBuilder.PasswordChangeFailed(passwordChangeFailed.ErrorDescription); } else if (e is EmailChangeFailedException) { message = MessageBuilder.EmailChangeFailed(); } Message = message; } else if (e is ServerNotRespondingException) { Message = MessageBuilder.ServerNotResponding(); } else if (e is InternalServerException) { Message = MessageBuilder.InternalServerError(); } } if (isSkipped) { Title = null; Message = null; LastException = null; } else if (Message == null) { Title = LastException.GetType().FullName; Message = LastException.Message; } else { Title = null; } StateHasChanged(); }); base.OnInitialized(); }