Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Looks at all the inner exceptions of the <paramref name="exception"/> parameter to find the
- /// most likely root cause of the exception. This works by skipping all registered exception types.
- /// </summary>
- /// <remarks>
- /// This method is not 100% accurate and should only be used to point a developer into the most likely direction.
- /// It should not be used to replace the Inner Exception stack of an exception, because this might hide required exception
- /// information.
- /// </remarks>
- /// <param name="exception">The exception that will provide the list of inner exeptions to examine.</param>
- /// <returns>
- /// The exception that most likely caused the exception to occur. If it can't find the root exception, it will return the
- /// <paramref name="exception"/> value itself.
- /// </returns>
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We have to catch exception. This method is used in exception handling code, so it must not fail.")]
- public static Exception GetRootException(this Exception exception)
- {
- Exception rootException = exception;
- try
- {
- while (true)
- {
- if (rootException == null)
- {
- rootException = exception;
- break;
- }
- rootException = rootException.InnerException;
- }
- }
- catch (Exception)
- {
- rootException = exception;
- }
- return rootException;
- }
Advertisement
Add Comment
Please, Sign In to add comment