Reputation: 34188
I was trying to capture exception details with the below code, but it is not giving error in details.
I was trying to download a file from our ftp server with code like ftp classes. When file was not found, I fire the above method and pass in the exception. Unfortunately the method printed the following:
Error occured and Error details as follows
Exception occured in file
Exception occured in method SyncRequestCallback
Exception occured in line & column number 0 0
Here is my code. How can I capture the exception details (e.g. line number, method where the exception was thrown, etc.)?
public static string GetException(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();
string strBody = "Hi,<br/><br/>";
strBody = strBody + "Error occured and Error details as follows<br/>";
strBody = strBody + "Error message " + ex.Message + "<br/>";
strBody = strBody + "Exception occured in file " + fileName + "<br/>";
strBody = strBody + "Exception occured in method " + methodName + "<br/>";
strBody = strBody + "Exception occured in line & column number " + line + " " + col + "<br/><br/>Thanks";
return strBody;
}
Thanks
Upvotes: 1
Views: 441
Reputation: 68400
In order to capture more information you'll need to deploy .pdb
files generated on build. This will allow you to know specific line where error happened.
As a side note, it seems to me that you're reinventing the wheel creating such a complex logic to log. There plenty of frameworks that will do the dirty job for you as log4net.
Upvotes: 4
Reputation: 45083
Calling ToString
on an exception generally generates a readable version of the public details. And only the details available to the exception will be shown, so if the app is not running in debug mode and doesn't have corresponding *.pdb files (or you don't have them to attach, after the fact) - and symbols loads - then you can't get detailed source information.
Upvotes: 1