Open enemaerke opened 3 years ago
Working a bit more with this in our context we are hitting this scenario (in a microservice setup):
ToStringDemystified()
or just the standard StackTrace
property, not having an immediate way picking:
ToStringDemystified()
does not return any stacktrace and is not usefull and we'd like to instead log the StackTrace
property that returns the serialization captured stacktrace.ToStringDemystified()
So we need a way to determine this. We could attempt to determine the number of stackframes, since they are 0 when having crossed the serialization boundary but that would require us to perform either new StackTrace(exception).FrameCount
or even new EnhancedStackTrace(exception).FrameCount
and thus potentially paying the penalty of obtaining a stacktrace twice. A way to avoid this would be to allow the EnhancedStackTrace
to also be demystified. So essentially:
var enhancedStackTrace = new EnhancedStackTrace(exc);
if (enhancedStackTrace.FrameCound > 0) {
return enhancedStackTrace.ToStringDemystified(exc); //proposed new method
}
return exc.StackTrace;
This new method hanging off of the EnhancedStackTrace
would essentially enable the ToStringDemystified
to skip new'ing up another instance of EnhancedStackTrace
and saving a stackframe capture. Would there be interest in such an extension?
Exceptions going through the serialization constructor stores the original stacktrace in a private
_stackTraceString
field (see https://referencesource.microsoft.com/#mscorlib/system/exception.cs,93). This original stacktrace is tested for and returned from the StackTrace property-getter in Exception (see https://referencesource.microsoft.com/#mscorlib/system/exception.cs,352 )From my observations, Demystifying an exception after serialization will not capture and Demystify this original stacktrace but will instead return a string that is the exception type and the exception message, no stacktrace included.
This is a quick repro (cheating a little bit with the reflection to avoid a serialization pass in this code):