StackFrame不会获取所有帧 [英] StackFrame does not get all of frames

查看:69
本文介绍了StackFrame不会获取所有帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了有效调试程序,我尝试编写获取异常的函数并通过电子邮件向我发送有关该异常的详细报告。

for effective debugging of my program, I tried to write function that gets an exception and emails me detailed reporting about the exception.

c#代码:

 public static bool SendExceptionMail(Exception ex, string[] mail, string title)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<table>");
        var st = new StackTrace(ex, true);
        builder.Append("<tr><td colspan='3'><h3>" + ex.GetType().Name + "</h3></td></tr>");
        builder.Append("<tr><td style='border: 1px solid #d8d8d8'>function name</td><td style='border: 1px solid #d8d8d8'>function path</td><td style='border: 1px solid #d8d8d8'>line number</td></tr>");
        StackFrame[] frames = st.GetFrames();
        for (int i = 0; i < frames.Length; i++)
        {
            builder.Append("<tr>");
            var frame = st.GetFrame;
            var line = frame.GetFileLineNumber();
            var name = frame.GetFileName();
            var functionName = frame.GetMethod().Name;
            builder.Append("<tr><td style='border: 1px solid #d8d8d8'>" + functionName + "</td><td style='border: 1px solid #d8d8d8'>" + name + "</td><td style='border: 1px solid #d8d8d8'>" + line + "</td></tr>");
            builder.Append("</tr>");
        }
        builder.Append("<tr><td colspan='3'>" + getInnerException(ex) + "</td></tr>");
        builder.Append("<tr><td colspan='3'>" + ex.Data + "</td></tr>");
        builder.Append("<tr><td colspan='3'>" + ex.StackTrace + "</td></tr>");
        builder.Append("<tr><td colspan='3'>" + DateTime.Now + "</td></tr>");
        builder.Append("</table>");
        return MailBll.Send("Exception in WS - " + frames[0].GetMethod().Name + " function", builder.ToString(), mail);
    }

private static string getInnerException(Exception ex)
{
        StringBuilder str = new StringBuilder();
        do
        {
            str.Append(ex.Message + "<br/>";
            ex = ex.InnerException;

        } while (ex != null);
        return str.ToString();
}

清除功能:

public TranStatus Clearing(Entities.Tran tran, int projectId, string Link = null)
    {
        string d = DateTime.Now.ToString();
        try
        {
            sendTranDetails(tran, projectId, d);
            if (!string.IsNullOrEmpty(tran.Token) && !string.IsNullOrEmpty(tran.CreditNum))
            {
                tran.Token = "";
            }
            tran.Ip = "";
            if ((string.IsNullOrEmpty(tran.CreditNum) || tran.CreditNum.Length < 7) && string.IsNullOrEmpty(tran.Token))
            {
                return db.getCardErrorMassege("478");
            }

            if (!string.IsNullOrEmpty(tran.CreditNum) && tran.CreditNum.Length > 4)
            {
                if (tran.CreditNum.Substring(0, 4) == "0404" || tran.CreditNum.Substring(0, 4) == "0403")
                {
                    tmobile m = db.Mobiles.GetById(int.Parse(tran.Comment2));
                    return db.getCardErrorMassege("479");
                }
            }
            tterminal terminal;
            tproject p = db.GetProjectById(projectId);
            tran.ProjectNumber = p.sNumProject;
            terminal = db.GetterminalByProjectId(projectId);
            tran.Total = (int)tran.Total;
            tran.CompanyId = (int)p.iCompanyId;
            fixTran(tran);
            string intot = Dal.Transaction(terminal.sTerminalId.Substring(0, 7), PasswordBll.DecodeFrom64(terminal.sPasswordService), tran);
            string creditStatus = db.getcreditStatusById(intot);
            sendEmailTran(d, intot + " - " + creditStatus + "<br/>");
            return db.getCardErrorMassege(intot.Substring(0, 3));
        }
        catch (Exception ex)
        {
            MailBll.SendExceptionMail(ex);
            return db.getCardErrorMassege("484");
        }
    }

该功能正常,但是有些功能我无法使用

the function works ok, but there are some functions that I cannot get the original function that the exception was thrown from it.

例如:当我得到 IndexOutOfRangeException的异常时;我收到了这封邮件:

for example: when I get exception of "IndexOutOfRangeException" I got this mail:

但我看不到Clearing函数中的哪一行引发了

but I don't see which line in Clearing function threw the exception and which file it was.

原因可能是什么?

推荐答案

Debug还是发布版本?

Debug or Release build?

如果您使用的是优化(发布)版本,则允许编译器删除简单函数并将其内联。目前,他们没有堆叠框架。为了获得良好的堆栈跟踪,您需要确保未启用任何优化。

If you are using a optimised (release) build, then the compiler is allowed to remove simple functions and in-line them. At this point, they won't have a stack frame. To get a good stack trace, you need to make sure no optimisations are enabled.

这篇关于StackFrame不会获取所有帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆