如何在.Net中实现这种堆栈跟踪?

时间:2011-08-21 03:22:23

标签: c# .net multithreading stack-trace

我的问题是一些代码以可重入的方式被调用,导致它崩溃并试图调试它可能被2个线程调用或者可以用同一个线程重入。我添加了一个提供Environment.StackTrace的日志,这是它在此消息结束时得到的内容。

我的困惑在于:

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

这些似乎是任何堆栈的顶部或开头。但请注意,堆栈跟踪实际上是在这些行之前开始的。事实上,上面的那一行是这样的:

at TickZoom.MBTFIX.MBTFIXSimulator.OnTick(Message quoteMessage, SymbolInfo symbol, Tick tick) in C:\Local\TickZoom\Project\Public\Providers\MBTFIX\MBTFIXProvider\MBTFIX\MBTFIXSimulator.cs:line 481

这是第481行:

if( trace) log.Trace("Sending tick: " + tick);

但该行只是一条日志消息,并且无法以任何可能的方式调用System.Threading.ThreadHelper.ThreadStart()。那么这个疯狂的堆栈跟踪是如何存在的呢?

at TickZoom.MBTFIX.MBTFIXSimulator.OnTick(Message quoteMessage, SymbolInfo symbol, Tick tick) in C:\Local\TickZoom\Project\Public\Providers\MBTFIX\MBTFIXProvider\MBTFIX\MBTFIXSimulator.cs:line 479
at TickZoom.FIX.FIXServerSymbolHandler.ProcessOnTickCallBack() in C:\Local\TickZoom\Project\Public\Providers\Common\ProviderUtil\FIX\FIXServerSymbolHandler.cs:line 301
at TickZoom.Threading.TaskLoop.Run() in C:\Local\TickZoom\Project\Engine\Parallel\TaskBase.cs:line 669
at TickZoom.Threading.TaskBase.Execute(ThreadInfo thread) in C:\Local\TickZoom\Project\Engine\Parallel\TaskBase.cs:line 213
at TickZoom.Threading.ParallelManager.ExecuteTasks(ThreadInfo thread) in C:\Local\TickZoom\Project\Engine\Parallel\ParallelManager.cs:line 685
at TickZoom.Threading.ParallelManager.Run() in C:\Local\TickZoom\Project\Engine\Parallel\ParallelManager.cs:line 632
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
at TickZoom.MBTFIX.MBTFIXSimulator.OnTick(Message quoteMessage, SymbolInfo symbol, Tick tick) in C:\Local\TickZoom\Project\Public\Providers\MBTFIX\MBTFIXProvider\MBTFIX\MBTFIXSimulator.cs:line 481
at TickZoom.FIX.FIXServerSymbolHandler.ProcessOnTickCallBack() in C:\Local\TickZoom\Project\Public\Providers\Common\ProviderUtil\FIX\FIXServerSymbolHandler.cs:line 301
at TickZoom.Threading.TaskLoop.Run() in C:\Local\TickZoom\Project\Engine\Parallel\TaskBase.cs:line 669
at TickZoom.Threading.TaskBase.Execute(ThreadInfo thread) in C:\Local\TickZoom\Project\Engine\Parallel\TaskBase.cs:line 213

1 个答案:

答案 0 :(得分:2)

正如您对问题的评论一样,当一个或多个堆栈跟踪一个接一个地输出时,会出现奇怪的堆栈跟踪:

Log.WriteLine(Environment.StackTrace)
try
{
    SomethingThatThrowsAnException();
}
catch (Exception e)
{
    Log.WriteLine(e.StackTrace); // better to do Log.WriteLine(e) to get the message
}