使用堆栈跟踪记录异常 [英] Exception Logging Using stack trace

查看:262
本文介绍了使用堆栈跟踪记录异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录异常详细信息
Filename,FileLineNo,方法名称(发生异常的地方)
使用StackTrace类可以正常工作
但是有时候我得到的文件行号和方法名称为null
发生异常时,我应如何始终跟踪异常详细信息


这是我的代码

I wanted to log the Exception details
Filename , FileLineNo, Method Name (where the exception occured)
This is working using the StackTrace class
But some times i am getting the File Line No and Method Name as null
How shall i track Exception Details all the time when an Exception occur


here is my code

Public Shared Sub LogException(ByVal ex As Exception)

Dim trace As Diagnostics.StackTrace = New Diagnostics.StackTrace(ex, True)
LogInfo(String.Format("Error Message :{0}  => Error In :{1}  => Line Number :{2} => Error Method:{3}",
                                  ex.Message, trace.GetFrame(0).GetFileName(),
                                  trace.GetFrame(0).GetFileLineNumber(),
                                  trace.GetFrame(0).GetMethod().Name))
End Sub





预先感谢





Thanks in advance

推荐答案

1)仅当使用调试信息编译代码时,源代码行/位置信息才可用.有关详细信息,请参见项目选项.

2)您可能会错过一些非常重要的信息;我看不到您是否使用Exception.InnerException属性.由于属性的类型也为System.Exception,因此返回的异常对象也可以具有内部异常,因此您需要递归地将其全部考虑.在某些情况下经常使用此功能.

同样,您也不要忘记每个调用堆栈的异常堆栈是分开的,因此您需要捕获每个线程中的所有异常,至少在每个线程的最上层堆栈帧中.

另请参阅我对马库斯对问题的评论.如果您需要真正全面的诊断,那么您的方法可能会很有用.

—SA
1) The source code line/position information is only available if you compile the code with the debug information. Please see project options for relevant detail.

2) You might miss some very important information; I cannot see if you use Exception.InnerException property. As property is also of the type System.Exception, the returned exception object can also have inner exception, so you need to consider them all, recursively. This feature is often used in some cases.

Also, you should not forget that the exception stacks are separate for each call stack, so you need to catch all exceptions in every thread, at least on the very top stack frame of each thread.

Please also see my comment to the Marcus''s comment to the question. Your approach could be useful if you need really comprehensive diagnostics.

—SA


这篇关于使用堆栈跟踪记录异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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