2013-10-09 12 views
7

Sto cercando di ottenere i dettagli da un'eccezione verificatasi nella mia applicazione web C# (4.0) asp.net. Ho il file .pdb nella cartella bin. Il seguente codice non funziona come previsto -Ottieni il file di origine, il metodo e la riga nell'eccezione

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     //throwing Exception 
     using (SqlConnection connection=new SqlConnection("")) 
     { 
      connection.Open(); 
     } 
    } 
    catch (Exception exception) 
    { 
     //Get a StackTrace object for the exception 
     StackTrace st = new StackTrace(exception, true); 

     //Get the first stack frame 
     StackFrame frame = st.GetFrame(0); //returns {PermissionDemand at offset 5316022 in file:line:column <filename unknown>:0:0} 

     //Get the file name 
     string fileName = frame.GetFileName(); //returns null 

     //Get the method name 
     string methodName = frame.GetMethod().Name; //returns PermissionDemand 

     //Get the line number from the stack frame 
     int line = frame.GetFileLineNumber(); //returns 0 

     //Get the column number 
     int col = frame.GetFileColumnNumber(); //returns 0    
    } 
} 

Cosa c'è che non va qui?

aggiornamento: "StackFrame frame = st.GetFrame (st.FrameCount - 1);" risolto questo problema.

+0

Fa eccezione. StackTrace aiuto? (http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx) – Samuel

+1

Ciò restituisce l'intera stringa di errore. Non lo voglio –

+0

Il tuo post inizia con "Sto cercando di ottenere dettagli da un'eccezione". La proprietà StackTrace restituisce una rappresentazione di stringa della traccia dello stack. Non restituisce l'intera stringa di errore. Questo sarebbe generato da exception.ToString(). Si prega di essere più specifici – Samuel

risposta

6
//Remove this line 
StackFrame frame = st.GetFrame(0); 

//Add this line 
StackFrame frame = st.GetFrame(st.FrameCount - 1); 
Problemi correlati