locked
How can i get the stack trace at runtime when Exception occurs ? RRS feed

  • Question

  • User1253338400 posted

    Hi ,

    I have a web application and when anb exception happens there is not enough information logged to tell me on whcih line the exception occurred because I cant log the stack trace to the event viewer logs. 

    There is in Visual Studio 2012 a method to get some information using 

    [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
    [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
    [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)

    System.Runtime.CompilerServices.CallerLineNumber] doesnt giove the line the exception occurred on but instead the line that the routine was called from.

    Is there a way or a property that logs the lien that the exception occurred on  in Release mode?

    Thanks

    Thursday, February 12, 2015 11:13 PM

Answers

  • User-434868552 posted

    @robby32

    robby, AFAIK, i know it also works in Release mode.   i could be wrong.

    one option that works for certain is to deploy your debug version.

    the downside is your code may be a little bigger and run a little slower.

    the upside is that it's very useful for staying on top of a deployed web application until you're sure it's solid.

    FWIW

    edit:

    Release mode works for me; the following statement has only been compiled in release mode.

    ViewBag.Message = "RELEASE " + robby32.Message + " ********** " + stacktrace;

    output:

    RELEASE Attempted to divide by zero. **********
     at WebApplication2.Controllers.HomeController.About()
     in h:\vs2013\ultimate\projects\WebApplication2\WebApplication2\Controllers\HomeController.cs:line 21

    as a further test, i recreated the above test as a new solution ... i created only a release only version, no debug version, with this line:

    ViewBag.Message = "RELEASE only version *** " + robby32.Message + " ********** " + stacktrace;

    i also rearranged the code so that line 21 is now line 25 and starting without debugging:

    RELEASE only version ***
    Attempted to divide by zero. **********
     at WebApplication3release.Controllers.HomeController.About()
     in h:\vs2013\ultimate\projects\WebApplication3release\WebApplication3release\Controllers\HomeController.cs:line 25

    end edit.

    edit #2:

    Note that i see a .pdb file; these files contain debugging information ... they are not AFAIK human readable; they likely contain line numbers (i could be wrong).

    robby, i've not found anything conclusive but so far it seems that line numbers work with both debug and release versions.

    the little test programs, i am running on a development machine.

    the one and only thing that i am certain about at the moment is that i have production web applications running on a web server that have line numbers in their stacktrace data; it's possible that i deployed a debug version ... i can not remember.  sorry.

    end edit2.

    edit #3:

    so much to know, so litte time ...  FWIW, i think i've confirmed that line numbers are possible in release mode.

    in Visual Studio, if you first select the project, then click the icon to show all files, you will see some folders that are otherwise hidden.

    the folder bin contains your executable code.

    the folder obj contains two subfolders:  Debug and Release.

    my "release only version" has a .dll file and a .pdb in obj/Release but not in obj/Debug -- for that reason, it appears that one's stacktrace can display line numbers even for Release versions.

    robby32, when you deploy your website, do you include the release .pdb file?

    end edit #3.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 6:16 PM

All replies

  • User-760709272 posted

    I explain why you're not seeing the line numbers in the following thread, and why you can't get them

    http://forums.asp.net/post/5852603.aspx

    http://forums.asp.net/t/2032605.aspx?Help+needed+to+understand+this+error+message

    If you're having problems debugging the code in release you'll need to add your own logging to trace what is happening in your code, what the content of variables are etc.  Look at something like log4net.

    Friday, February 13, 2015 3:48 AM
  • User-1587158655 posted

    Hi,

    How about use breakpoint(s) , then press F11 to step through the codes in order to find which the error line.

    Friday, February 13, 2015 4:18 AM
  • User-434868552 posted

    @robby32

    you can try this:

    public void CrashAbout(Int32 one)
    {
        Double blowup = 100 / (1 - one);  // this will crash if    one == 1
    }

    in an ASP.NET MVC application:

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        try
        {
            CrashAbout(1);
        }
        catch (Exception robby32)
        {
            String stacktrace = robby32.StackTrace;
            ViewBag.Message   = robby32.Message + " ********** " + stacktrace;
            return View();
        }
        return View();
    }

    the view displays line 21:

    About.
    Attempted to divide by zero. **********
    at WebApplication2.Controllers.HomeController.About()
    in h:\vs2013\ultimate\projects\WebApplication2\WebApplication2\Controllers\HomeController.cs:line 21

    Friday, February 13, 2015 3:25 PM
  • User1253338400 posted

    Hi<br>
    this will work in debug mode but i was after a solution in release mode For a web appbr>
    <br>
    Thanks<br>
    <br>
    Friday, February 13, 2015 4:36 PM
  • User-434868552 posted

    @robby32

    robby, AFAIK, i know it also works in Release mode.   i could be wrong.

    one option that works for certain is to deploy your debug version.

    the downside is your code may be a little bigger and run a little slower.

    the upside is that it's very useful for staying on top of a deployed web application until you're sure it's solid.

    FWIW

    edit:

    Release mode works for me; the following statement has only been compiled in release mode.

    ViewBag.Message = "RELEASE " + robby32.Message + " ********** " + stacktrace;

    output:

    RELEASE Attempted to divide by zero. **********
     at WebApplication2.Controllers.HomeController.About()
     in h:\vs2013\ultimate\projects\WebApplication2\WebApplication2\Controllers\HomeController.cs:line 21

    as a further test, i recreated the above test as a new solution ... i created only a release only version, no debug version, with this line:

    ViewBag.Message = "RELEASE only version *** " + robby32.Message + " ********** " + stacktrace;

    i also rearranged the code so that line 21 is now line 25 and starting without debugging:

    RELEASE only version ***
    Attempted to divide by zero. **********
     at WebApplication3release.Controllers.HomeController.About()
     in h:\vs2013\ultimate\projects\WebApplication3release\WebApplication3release\Controllers\HomeController.cs:line 25

    end edit.

    edit #2:

    Note that i see a .pdb file; these files contain debugging information ... they are not AFAIK human readable; they likely contain line numbers (i could be wrong).

    robby, i've not found anything conclusive but so far it seems that line numbers work with both debug and release versions.

    the little test programs, i am running on a development machine.

    the one and only thing that i am certain about at the moment is that i have production web applications running on a web server that have line numbers in their stacktrace data; it's possible that i deployed a debug version ... i can not remember.  sorry.

    end edit2.

    edit #3:

    so much to know, so litte time ...  FWIW, i think i've confirmed that line numbers are possible in release mode.

    in Visual Studio, if you first select the project, then click the icon to show all files, you will see some folders that are otherwise hidden.

    the folder bin contains your executable code.

    the folder obj contains two subfolders:  Debug and Release.

    my "release only version" has a .dll file and a .pdb in obj/Release but not in obj/Debug -- for that reason, it appears that one's stacktrace can display line numbers even for Release versions.

    robby32, when you deploy your website, do you include the release .pdb file?

    end edit #3.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2015 6:16 PM
  • User1253338400 posted

    fantastic thanks heaps , I have used your ideas and they worked 

    thanks

    Saturday, February 14, 2015 2:02 AM
  • User-434868552 posted

    @robby32   you're welcome.

    Co-incidentally, i just now thought of an interesting experiment ... before starting without debug my release only version, i permanently deleted the .pdb file from my hard disk ... although i did not rebuild, Visual Studio recreated the .pdb file with the same time stamp as the corresponding .dll file.

    Glad this worked for you.

    Saturday, February 14, 2015 3:02 AM