by Edward
01 October 2009 06:10
There might be times when you will need the name of the method that is throwing an error, or when logging errors, exceptions etc. The following example uses reflection and the System.Diagnostics namespace.
Example code:
using System.Diagnostics;
try
{
//code here
}
catch(exception ex)
{
// get call stack
StackTrace stack = new StackTrace();
// get calling method name
// code here to log or write the error
//custom error handling
Error.LogError("An exception has been thrown", "Method: " + stack.GetFrame(1).GetMethod().Name, ex))
}