Skip to content

Commit 9be6251

Browse files
authored
Merge pull request #657 from twpol/feature/fix-hang-stacks
fix: Correctly merge exception and current stack traces in logger
2 parents 0ff87d8 + 35913de commit 9be6251

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

Source/Orts.Simulation/Common/ErrorLogger.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,12 @@ void TraceEventInternal(TraceEventCache eventCache, string source, TraceEventTyp
153153
// full stack to this trace call, which goes via the catch block at the same level as the try block. We'd prefer to have the whole stack, so we need to find the
154154
// join and stitch the stacks together.
155155
var error = args[0] as Exception;
156-
var errorStack = new StackTrace(args[0] as Exception);
157-
var errorStackLast = errorStack.GetFrame(errorStack.FrameCount - 1);
158-
var catchStack = new StackTrace();
159-
var catchStackIndex = 0;
160-
while (catchStackIndex < catchStack.FrameCount && errorStackLast != null && catchStack.GetFrame(catchStackIndex).GetMethod().Name != errorStackLast.GetMethod().Name)
161-
catchStackIndex++;
162-
catchStack = new StackTrace(catchStackIndex < catchStack.FrameCount ? catchStackIndex + 1 : 0, true);
156+
var errorStack = error.ToString().Replace("\r", "").Split('\n');
157+
var catchStack = new StackTrace(true).ToString().Replace("\r", "").Split('\n');
158+
var catchIndex = Array.IndexOf(catchStack, errorStack[errorStack.Length - 1]);
163159

164160
output.AppendLine(error.ToString());
165-
output.AppendLine(catchStack.ToString());
161+
if (catchIndex >= 0) output.AppendLine(String.Join(Environment.NewLine, catchStack, catchIndex + 1, catchStack.Length - catchIndex - 1));
166162
}
167163
else
168164
{

Source/RunActivity/Viewer3D/Processes/WatchdogProcess.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ bool StacksAreWaits()
312312

313313
class ThreadWatchdogException : Exception
314314
{
315+
public override string ToString()
316+
{
317+
return $"{GetType()}: {Message}{Environment.NewLine}{StackTrace}";
318+
}
319+
315320
public override string StackTrace
316321
{
317322
get

0 commit comments

Comments
 (0)