Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am using code which is mentioned below to get form name and line no. in catch block when any exception occurs in application:

My code is working properly in local machine but when hosted on server its not working. Can anybody help me to solve this issue in my application.


Thanks a lot.

What I have tried:

C#
private static String[] GetExceptionDetail(Exception e)
{

    string[] strArr = new string[2] { "", "" };
    try
    {

        //Line No.  
        strArr[0] = Convert.ToString(e.StackTrace.Substring(e.StackTrace.LastIndexOf('')));

        //Form Name
        strArr[1] = Convert.ToString(e.StackTrace.Substring(e.StackTrace.LastIndexOf('\\') + 1, e.StackTrace.LastIndexOf(':') - (e.StackTrace.LastIndexOf('\\') + 1)));
    }
    catch
    {
        //Stack trace is not available!
    }
    return strArr;
}
Posted
v2

1 solution

Log the whole exception on the server and check that your parsing works as you expect it to. Why doesn't your exception handler (the original catch) just put this.Name into strArr[0]? It would save you some parsing. And why not have whole stack trace instead of line number?

Anyhow, there is StackTrace class that you can use
StackTrace Class (System.Diagnostics)
This will work if you have .pdb files along with your solution files.

C#
try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}
 
Share this answer
 
Comments
aarif moh shaikh 2-Mar-16 2:40am    
+5
Priyanka Tiwari001 3-Mar-16 1:08am    
Dear Sinisa thank you so much for your solution but with this code also I am facing same problem i.e. its working on local but not working on server.

Am I making any mistake or I should do some settings in server to get stacktrace, plz suggest me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900