从C#打印堆栈跟踪信息 [英] Print stack trace information from C#

查看:377
本文介绍了从C#打印堆栈跟踪信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一些错误在我们的产品操作的一部分,我们希望某些转储的堆栈跟踪信息。然而,我们的经验,很多用户会简单地采取错误消息对话框的截图,而不是向我们报告全文可从程序副本,因此我想在这对话框中提供一些最起码的堆栈跟踪信息。

As part of some error handling in our product, we'd like to dump some stack trace information. However, we experience that many users will simply take a screenshot of the error message dialog instead of sending us a copy of the full report available from the program, and thus I'd like to make some minimal stack trace information available in this dialog.

在我的机器上的.NET堆栈跟踪看起来是这样的:

A .NET stack trace on my machine looks like this:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path)
at LVKWinFormsSandbox.MainForm.button1_Click(Object sender, EventArgs e) in C:\Dev\VS.NET\Gatsoft\LVKWinFormsSandbox\MainForm.cs:line 36

我有这样的疑问:

I have this question:

格式看起来是这样的:

at <class/method> [in file:line ##]

然而,的和的中的关键词,我想,如果他们运行这些将被本地化,也就是说,一个挪威.NET运行时,而不是英语一个我已经安装

However, the at and in keywords, I assume these will be localized if they run, say, a norwegian .NET runtime instead of the english one I have installed.

有什么办法,我挑除了在语言无关的方式来解决此堆栈跟踪,这样我就可以只显示那些有此项目的文件和行号?

Is there any way for me to pick apart this stack trace in a language-neutral manner, so that I can display only the file and line number for those entries that have this?

在换句话说,我想从上面的文字这样的信息:

In other words, I'd like this information from the above text:

C:\Dev\VS.NET\Gatsoft\LVKWinFormsSandbox\MainForm.cs:line 36

您可以给任何意见会有所帮助。

Any advice you can give will be helpful.

推荐答案

您应当能够说得到一个堆栈跟踪对象,而不是字符串

You should be able to get a StackTrace object instead of a string by saying

var trace = new System.Diagnostics.StackTrace(exception);

您可以再看看自己的帧不依赖于框架的格式。

You can then look at the frames yourself without relying on the framework's formatting.

另请参阅:堆栈跟踪参考

这篇关于从C#打印堆栈跟踪信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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