Get Source file,method and line in c# exception

protected void Button1_Click(object sender, EventArgs e)
//throwing Exception
(SqlConnection connection=new SqlConnection(""))
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(st.FrameCount - 1);

//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();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s