I have the following code:
protected string formatException(Exception e)
{
var exError = "<form>";
if (e == null)
{
throw new ArgumentNullException("e");
}
exError += "<fieldset><legend><a href='#'>" +
"<span class='show-expanded'>collapse message</span>" +
"<span class='show-collapsed'>expand message</span>" +
"</a></legend><p>" + e.Message + "</p></fieldset>";
exError += "<fieldset><legend><a href='#'>" +
"<span class='show-expanded'>collapse trace</span>" +
"<span class='show-collapsed'>expand trace</span>" +
"</a></legend><p>" + e.StackTrace + "</p></fieldset>";
if (e.InnerException != null)
{
// same functionality but for the inner exception and the InnerException.InnerException
}
return exError + "</form>";
}
When called it formats the exception message. However I would like to make it include the InnerException
and the InnerException.InnerException
Is there some way I could do this recursively or would it be better to put the message format in another function and call that?
Here's what I'd do:
protected string formatException(Exception e)
{
Func<string, string> createFieldSet =
t =>
"<fieldset><legend><a href='#'>" +
"<span class='show-expanded'>collapse message</span>" +
"<span class='show-collapsed'>expand message</span>" +
"</a></legend><p>" + t + "</p></fieldset>";
var exError = new StringBuilder("<form>");
if (e == null)
{
throw new ArgumentNullException("e");
}
while (e != null)
{
exError.AppendLine(createFieldSet(e.Message));
exError.AppendLine(createFieldSet(e.StackTrace));
e = e.InnerException;
}
exError.AppendLine("</form>");
return exError.ToString();
}