diff --git a/src/testhost.x86/TestHostTraceListener.cs b/src/testhost.x86/TestHostTraceListener.cs index 0832b07d6d..d5dd7b78de 100644 --- a/src/testhost.x86/TestHostTraceListener.cs +++ b/src/testhost.x86/TestHostTraceListener.cs @@ -49,7 +49,8 @@ private static DebugAssertException GetException(string? message) var debugMethodFound = false; var frameCount = 0; MethodBase? method = null; - foreach (var f in stack.GetFrames()) + var frames = stack.GetFrames(); + foreach (var f in frames) { var m = f?.GetMethod(); var declaringType = m?.DeclaringType; @@ -65,7 +66,7 @@ private static DebugAssertException GetException(string? message) } } - var stackTrace = string.Join(Environment.NewLine, stack.ToString().Split(Environment.NewLine).TakeLast(frameCount)); + var stackTrace = new StackTrace(frames.TakeLast(frameCount)).ToString(); var methodName = method != null ? $"{method.DeclaringType?.Name}.{method.Name}" : ""; var wholeMessage = $"Method {methodName} failed with '{message}', and was translated to {typeof(DebugAssertException).FullName} to avoid terminating the process hosting the test.";