这个堆栈跟踪是如何在.net中实现的?

本文关键字:net 实现 堆栈 跟踪 | 更新日期: 2023-09-27 18:06:09

我的问题是一些代码正在以可重入的方式调用,这导致它崩溃,并试图调试它如何可能被2个线程调用或与同一线程可重入。我添加了一个日志,给出了环境。这是它在消息末尾得到的结果

我的困惑在这行之后:

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

这个堆栈跟踪是如何在.net中实现的?

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

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