如何拆分堆栈跟踪行成的命名空间,类,方法文件和行号? [英] How to split a stacktrace line into namespace, class, method file and line number?
问题描述
C#堆栈跟踪采取以下形式:
C# stack traces take the following form:
at Foo.Core.Test.FinalMethod(Doh doh) in C:\Projects\src\Core.Tests\Test.cs:line 21
at Foo.Core.Test.AnotherMethod(Bar bar)
at Foo.Core.Test.AMethod() in C:\Projects\src\Core.Tests\Test.cs:line 6
at Foo.Core.Test.<>c__DisplayClass7.<SomeAnonDelegate>b__6(Object _) in C:\Projects\src\Core.Tests\Test.cs:line 35
如何能我检索命名空间,类,方法,从每一行文件和行号?
How can I retrieve namespace, class, method, file and line number from each line?
- 是否有任何现有的类来做到这一点?
- 如果没有这将是最好的办法?
- 正则表达式?我将如何贪婪地匹配的命名空间,但离开的类和方法?
- 自定义解析器?
- Are there any existing classes to do this?
- If not what would be the the best approach?
- Regex? How would I greedily match the namespace but leave the class and method?
- Custom parser?
希望得到一些想法和输入
Would appreciate some ideas and input.
推荐答案
如果您是从的StackTrace ,那么你可以遍历的 StackFrames 通过的的getFrame 并调用 GetMethod ,的GetFileName 和的 GetFileLineNumber 。命名空间和类可以从方法进行检索。
If you're getting this from a StackTrace, then you can loop through the StackFrames via GetFrame and call GetMethod, GetFileName, and GetFileLineNumber. Namespace and class can be retrieved from the method.
修改结果
在回答第一个注释(不幸的是,我们从Exception.StackTrace得到的痕迹的),你可以调用堆栈跟踪(例外)< 。/ A>构造
EDIT
In response to the first comment (unfortunately we get the traces from Exception.StackTrace), you can call the StackTrace(Exception) constructor.
修改结果
我已经联系到该构造,而不是 - 的StackTrace(Exception,bool)。
这篇关于如何拆分堆栈跟踪行成的命名空间,类,方法文件和行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!