Save call stack

时间:2019-03-19 14:51:26

标签: c# debugging

Short version: Is there a way to somehow save the current call stack and store it for later?

Context: This issue seems like it would be so common that I might just be missing the correct search terms.

A certain class needs to read a variable from a server. I just send the request to the server using an API and at some point in the future a separate thread will get the reply and call some follow up method on the original caller class.

Simple enough, but there might be errors: either the variable I'm trying to get doesn't exist, is an incorrect type or something else. In those cases the best I can output to a debug log can be something like: "Unable to find variable X" and a stack trace of the worker. What I would really like having is a stack trace of the original caller, that I can store somewhere and print out in case there is an error so I can narrow down issues instead of having to hunt down which caller it is.

1 个答案:

答案 0 :(得分:4)

Environment.StackTrace would give you the current stack trace.