如何最好地将堆栈跟踪转换为HTML(使用.NET - C#)

ant*_*nio 6 .net html c#

您好,有一个类可以进行漂亮的转换吗?

Ree*_*sey 7

内置没有任何东西,但它会相当容易.

抓住StackTrace:

// Create trace from exception
var trace = new System.Diagnostics.StackTrace(exception);

// or for current code location
var trace = new System.Diagnostics.StackTrace(true);
Run Code Online (Sandbox Code Playgroud)

完成后,只需迭代堆栈帧,然后根据需要对其进行格式化.

有很多方法可以将其格式化为HTML - 这实际上取决于您希望它的外观.基本概念是:

int frameCount = trace.Framecount;
for (int i=0;i<frameCount;++i)
{
     var frame = trace.GetFrame(i);
     // Write properties to formatted HTML, including frame.GetMethod()/frame.GetFileName(), etc.
     // The specific format is really up to you.
}
Run Code Online (Sandbox Code Playgroud)