有没有一种方法可以获取作用域中变量的名称和值?

I'm trying to write a handler for logger that sends events to sentry. I'd like to include as context the variables in scope for easier debugging. Is there a way to do this ( short of a WriteHeapDump ) ?

Is there a way to get the names and values of variables in scope?

No.

Sort of. You can use github.com/jwells131313/goethe to set thread-local-variables on goethe threads (special threads that have a thread id). It's sort of frowned upon though.