1

When asp.net throws an exception, the exception object includes a stack trace property, but it's one big long string. Is there a way to get it as an array or some other structure that can be examined programmatically?

My immediate problem is that I've got a bug that shows up in an admin function now and then. It's hard to reproduce, so I wanted to say that when it happens, I catch the exception and search the stack for the lowest level statement that is within my code and display it, rather than dumping the whole stack. Okay, maybe this is more trouble than it's worth and I should just dump the stack, find and fix the bug, and then rip out the stack dump. But now I'm curious if it's possible.

RE duplicate: No, that's not my question. I'm not asking how to get a stack trace without throwing an exception. I'm asking, given that I have caught an exception, can I get the stack trace as an array or some other structure rather than as a string blob.

Jay
  • 26,876
  • 10
  • 61
  • 112

2 Answers2

0

My website has two options for reporting errors from exceptions.

  1. error email
  2. error log table

The error email option is probably the easiest to implement. The email body can be as long as you want with error information extracted from the exception object.

Defining an error email distribution list as a comma delimited string in the <appSettings> section of your web.config file can facilitate future updates of who is and who is not informed of website errors.

JohnH
  • 1,920
  • 4
  • 25
  • 32
  • Yes, I've done it in several other applications. But my question isn't how to "deliver" the stack trace, but rather that I wanted to have a program manipulate it and pull out the part that is most likely to be of interest. As I said in my original question, arguably this is a waste of time, I'd spend more time writing the code to examine the stack trace then to just eyeball it and figure out what I need until I've solved the problem. But I got to thinking that even if it isn't the best solution in this particular case, maybe it's a good thing to know how to do for future reference. – Jay Sep 26 '16 at 19:13
  • You raise a good point about the effort vs. reward of creating customized code that displays the stack trace for later analysis. For my website, the reward of timely and informative error emails was well worth the effort. The methods used to generate those emails continue to evolve.Your mileage may vary. – JohnH Sep 26 '16 at 20:46
0

The best thing to do is email yourself when there is an exception. you may be able to read it easier in an email and never miss an error this way.

C#:

   try
    {
        dv = dt.DefaultView; 
    }
    catch (Exception ex)
    {
        var username = getusername();
        Globals.SendError(username, ref ex);
        dv= null;
    }

Then finally here is how to send the email:

public void SendError(string username, ref Exception ex)
{
    var error = string.Empty;
    try
    {
        username = username.Trim();
        var site = ex.TargetSite;
        var functionName = site == null ? null : site.Name;

        error =
            string.Format(
                "Error in (your application name here).\r\nFunction Name: {0}. Failed.\r\nStackTrace: {1}.\r\nMessage: {2}.\r\nDateTime: {3}.",
                functionName, ex.StackTrace.Trim(), ex.Message.Trim(), DateTime.Now);

        if (string.IsNullOrEmpty(username))
            error += "\r\nEOM.";
        else
            error += string.Format("\r\nUsername: {0}\r\nEOM.", username);

        error = error.Trim();

        //If there's an error try to send an email letting me know.
        var client = new SmtpClient("mail.xyz.com", 25); //replace xyz with your domain
        var mailMessage = new MailMessage("yourEmailHere@email.com", "yourEmailHere@email.com",
            "Error in (Your application name here) Report", error);
        client.Send(mailMessage);
        client.Dispose();
        mailMessage.Dispose();
    }
    catch (Exception)
    {
        // ignored
    }

    UpdateErrorHistory(error, username);
}
cpeterson
  • 63
  • 1
  • 9