ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToStringReccurent

Sometimes it is required to collect exception information in textual format. This method serializes general info about exception and all included exceptions reccursively. I'm using this for sending email error reports.

Source

public static string ToStringReccurent(this Exception exception)
{
	if (exception == null)
	{
		return "empty";
	}
	return string.Format
		("Exception: {0}\nMessage: {1}\nStack Trace: {2}\nInner {3}", exception.GetType(),
			exception.Message, exception.StackTrace, exception.InnerException.ToStringReccurent());
}

Example

private void appDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
	e.Handled = true;
	MessageBox.Show("Application encountered an error, sorry for inconvinience.", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
	Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", DeveloperEMail,
		"StatusFlags Exception", e.Exception.ToStringReccurent().UrlEncode()));
	Shutdown();
}

Author: Andriy Kvasnytsya

Submitted on: 22 mei 2009

Language: C#

Type: System.Exception

Views: 5325