Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

c#

.net

Hello is there a class that does a pretty conversion?

like image 415
antonio Avatar asked Sep 05 '09 02:09

antonio


People also ask

How do I print a full stack trace?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

What is StackTrace C#?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

How do I save a stack trace?

If you want to save a stack trace for later use or create a link to it, click the Share button store the stack trace. Sharing stack traces on elmah.io works pretty much like gists on GitHub, but are nicely formatted and provides the visitor of your stack trace with the options for copying and saving a stack trace.


1 Answers

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.
}
like image 119
Reed Copsey Avatar answered Oct 04 '22 02:10

Reed Copsey