Get Source file,method and line in c# asp.net exception

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(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();
}
}
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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