简体   繁体   中英

How to best convert a stack trace into HTML (using .NET - C#)

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

There isn't anything built in, but it would be fairly easy.

Just grab the StackTrace :

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

// or for current code location
var trace = new System.Diagnostics.StackTrace(true);

Once you have this, just iterate the stack frames, and format them as desired.

There would be lots of ways to format this into HTML - it really depends on how you want it to look. The basic concept would be:

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重新发明轮子。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM