Missing exception stack recorded in logs

Source: Internet
Author: User

The project has the following scenario:

Use reflection to process all business calls. Use try-catch to process exceptions in the reflection call point and record exception information to logs. The log records are asynchronous.


Problem:

The stacktrace of the exception recorded in the log is different from the stacktrace of the exception thrown at the time of debug.


Cause:

Because the record logs are asynchronous, if the record logs occur before throw, the stacktrace that records exceptions in the logs is correct (all stacktraces from the true occurrence point to the throw point ); if the logged log occurs after throw, the stacktrace that records exceptions in the log is incorrect (only stacktrace from the current throw point to the code entry point );
Because the thrown exception (or reference of this exception) is the exception instance used to record logs, the throw action will re-generate stacktrace for the thrown exception (which is the same as the exception instance for logging), so the exception stacktrace used to record the log is re-generated.


(Note 1: stacktrace is the read-only attribute of exception. stacktrace is generated for thrown exceptions during throw)
(Note 2: this problem should not occur if the log is synchronized and logs are logged first and then throw)


Solution:

    • If you only need to record the most internal exception (abnormal occurrence point), you can first peel out the internal exception in catch, record the exception log, and then copy the exception to a copy, replica of throw exception;
    • If you want to record the complete call stack (including reflection exceptions) and throw the most internal exception, you can first record the exception (including the complete call stack) and then strip the most internal exception, copy a copy of the exception and a replica of the throw exception;
    • If you want to record the complete call stack and throw the original exception, you can record the exception (including the complete call stack) first ), then, recursively copy all the abnormal copies on the exception chain and combine the new abnormal chain (the Deep copy of the original exception chain) in the original order, and throw the copy of the exception chain. However, this method will lose all stacktrace of the intermediate node of the exception chain;

 

♦Method used:

 1 // 深赋值异常链上的所有异常,但是会丢失异常链上非叶子节点的异常的StackTrace。最内部的异常信息会完整保留,因为最内部的异常不是副本 2         public static Exception CloneInnerExceptionRecursive(Exception ex) 3         { 4             Exception exception = null; 5  6             if (ex.InnerException == null) 7             { 8                 exception = ex; 9             }10             else11             {12                 Stack<Exception> stack = new Stack<Exception>();13                 stack.Push(ex);14                 Exception innerException = ex.InnerException;15                 while (innerException != null)16                 {17                     if (innerException.InnerException == null)18                     {19                         break;20                     }21                     stack.Push(innerException);22                     innerException = innerException.InnerException;23                 }24 25                 try26                 {27                     Exception chain = innerException;28                     Exception temp = null;29                     while (stack.Count > 0)30                     {31                         temp = stack.Pop();32                         chain = CloneException(temp, chain);33                     }34                     exception = chain;35                 }36                 catch37                 {38                 }39 40                 if (exception == null || exception.InnerException == null)41                 {42                     exception = ex.InnerException;43                 }44             }45 46             return exception;47         }
1 // 复制异常2         public static Exception CloneException(Exception ex, Exception inner)3         {4             return (Exception)ex.GetType().GetConstructor(new Type[] { typeof(string), typeof(Exception) }).Invoke(new object[] { ex.Message, inner });5         }

 

Missing exception stack recorded in logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.