Per vedere lo stacktrace per un dato eccezione, utilizzare e.StackTrace
Se avete bisogno di informazioni più dettagliate, è possibile utilizzare la classe System.Diagnostics.StackTrace (qui è un codice per provare):
try
{
throw new Exception();
}
catch (Exception ex)
{
//Get a StackTrace object for the exception
StackTrace st = new StackTrace(ex, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0);
//Get the file name
string fileName = frame.GetFileName();
//Get the method name
string methodName = frame.GetMethod().Name;
//Get the line number from the stack frame
int line = frame.GetFileLineNumber();
//Get the column number
int col = frame.GetFileColumnNumber();
}
Questo funzionerà solo se è disponibile un file pdb per l'assembly. Vedi le proprietà del progetto - scheda build - Avanzate - Selezione informazioni di debug per assicurarti che ci sia un file pdb.
fonte
2010-08-06 17:42:13
Come si compila anche il codice? –
non mi sono appena inventato per mostrare come esempio – Crash893
possibile duplicato http://stackoverflow.com/questions/8338495/how-to-get-error-line-number-of-code-using-try-catch –