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

查看:61
本文介绍了从 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:DevVS.NETGatsoftLVKWinFormsSandboxMainForm.cs:line 36

我有这个问题:

格式看起来是这样的:

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

但是,atin 关键字,如果它们运行,例如,挪威 .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:DevVS.NETGatsoftLVKWinFormsSandboxMainForm.cs:line 36

您可以提供的任何建议都会有所帮助.

Any advice you can give will be helpful.

推荐答案

你应该能够得到一个 StackTrace 对象而不是一个字符串

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.

另见:StackTrace 参考

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

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