From 774fbe0e13753613747b63f5ec378436e674b9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 16 Jun 2025 10:03:20 +0200 Subject: [PATCH] Fix stack trace for Trace.Fail and Debug.Fail --- src/testhost.x86/TestHostTraceListener.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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.";