There are .NET Framework classes that will give you the call stack information at a particular point. Call this method inside the catch block.
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
public static void TraceCaughtException( Exception e )
{
StackTrace st = new StackTrace( 1, true );
StackFrame sf = st.GetFrame( StackTrace.METHODS_TO_SKIP );
if ( sf != null )
{
MethodBase mb = sf.GetMethod();
Trace.WriteLine( e.ToString() );
Trace.WriteLine( string.Format( "caught in File {0}, line {1}",
sf.GetFileName(), sf.GetFileLineNumber() ) );
StringBuilder sb = new StringBuilder();
sb.AppendFormat( "in method {0}.{1}(",
mb.ReflectedType.Name, mb.Name );
bool first = true;
foreach ( ParameterInfo pi in mb.GetParameters() )
{
sb.AppendFormat( first ? "{0} {1}" : ", {0} {1}",
pi.ParameterType.Name, pi.Name );
first = false;
}
sb.Append( ");" );
Trace.WriteLine( sb.ToString() );
}
}
Contributed from George Shepherd's Windows Forms FAQ