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

问题描述 投票:6回答:2

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

c# .net html
2个回答
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);

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

有很多方法可以将其格式化为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.
}

0
投票

这不是一个新问题,但我宁愿建议使用开源nuget,例如https://github.com/atifaziz/StackTraceFormatter比从头开始创建HTML重新发明轮子。

© www.soinside.com 2019 - 2024. All rights reserved.