您好,有一个类可以进行漂亮的转换吗?
内置没有任何东西,但它会相当容易。
抓住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.
}
这不是一个新问题,但我宁愿建议使用开源nuget,例如https://github.com/atifaziz/StackTraceFormatter比从头开始创建HTML重新发明轮子。