как это. Я сделал несколько изменений, но это довольно близко к тому, что делает мс.
// reverse the stack
private static Stack<Exception> GenerateExceptionStack(Exception exception)
{
var exceptionStack = new Stack<Exception>();
// create exception stack
for (Exception e = exception; e != null; e = e.InnerException)
{
exceptionStack.Push(e);
}
return exceptionStack;
}
// render stack
private static string GenerateFormattedStackTrace(Stack<Exception> exceptionStack)
{
StringBuilder trace = new StringBuilder();
try
{
// loop through exception stack
while (exceptionStack.Count != 0)
{
trace.Append("\r\n");
// render exception type and message
Exception ex = exceptionStack.Pop();
trace.Append("[" + ex.GetType().Name);
if (!string.IsNullOrEmpty(ex.Message))
{
trace.Append(":" + ex.Message);
}
trace.Append("]\r\n");
// Load stack trace
StackTrace stackTrace = new StackTrace(ex, true);
for (int frame = 0; frame < stackTrace.FrameCount; frame++)
{
StackFrame stackFrame = stackTrace.GetFrame(frame);
MethodBase method = stackFrame.GetMethod();
Type declaringType = method.DeclaringType;
string declaringNamespace = "";
// get declaring type information
if (declaringType != null)
{
declaringNamespace = declaringType.Namespace ?? "";
}
// add namespace
if (!string.IsNullOrEmpty(declaringNamespace))
{
declaringNamespace += ".";
}
// add method
if (declaringType == null)
{
trace.Append(" " + method.Name + "(");
}
else
{
trace.Append(" " + declaringNamespace + declaringType.Name + "." + method.Name + "(");
}
// get parameter information
ParameterInfo[] parameters = method.GetParameters();
for (int paramIndex = 0; paramIndex < parameters.Length; paramIndex++)
{
trace.Append(((paramIndex != 0) ? "," : "") + parameters[paramIndex].ParameterType.Name + " " + parameters[paramIndex].Name);
}
trace.Append(")");
// get information
string fileName = stackFrame.GetFileName() ?? "";
if (!string.IsNullOrEmpty(fileName))
{
trace.Append(string.Concat(new object[] { " in ", fileName, ":", stackFrame.GetFileLineNumber() }));
}
else
{
trace.Append(" + " + stackFrame.GetNativeOffset());
}
trace.Append("\r\n");
}
}
}
catch
{
}
if (trace.Length == 0)
{
trace.Append("[stack trace unavailable]");
}
// return html safe stack trace
return HttpUtility.HtmlEncode(trace.ToString()).Replace(Environment.NewLine, "<br>");
}