为了有效调试我的程序,我尝试编写获取异常的函数,并通过电子邮件向我发送有关异常的详细报告。
c#code:
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");
}
}
该函数运行正常,但是有一些函数我无法获得从中抛出异常的原始函数。
例如:当我得到&#34; IndexOutOfRangeException&#34;的异常时我收到了这封邮件:
但我不知道清算功能中的哪一行引发了异常以及它是哪个文件。
可能是什么原因?
答案 0 :(得分:0)
调试或发布版本?
如果您正在使用优化(发布)版本,则允许编译器删除简单函数并对其进行内联。此时,他们没有堆叠框架。要获得良好的堆栈跟踪,您需要确保未启用任何优化。