[英]How can I get useful WPF .NET error information from a user's machine?
I have a WPF application that's crashing once I get it onto machines that do not have a development environment installed-- if this is a dupe, I'm welcome to closing, but I my search-fu is failing to find an equivalent question. 我有一个WPF应用程序崩溃,一旦我把它安装到没有安装开发环境的机器上 - 如果这是一个骗局,我欢迎关闭,但我的搜索功能未能找到一个相同的问题。 It appears that I'm getting a XamlParseException, but nothing more useful than that. 我似乎得到了一个XamlParseException,但没有比这更有用了。 I need to get useful information. 我需要获得有用的信息。
Going through the Windows 7 event logs gives me this error log: 浏览Windows 7事件日志会给我这个错误日志:
Fault bucket , type 0
Event Name: CLR20r3
Response: Not available
Cab Id: 0
Problem signature:
P1: MyApp.exe
P2: 1.0.0.0
P3: 4b88323d
P4: PresentationFramework
P5: 3.0.0.0
P6: 4a174fbc
P7: 624f
P8: e1
P9: System.Windows.Markup.XamlParse
P10:
Attached files:
C:\Users\Mark\AppData\Local\Temp\WER7DC.tmp.WERInternalMetadata.xml
These files may be available here:
C:\Users\Mark\AppData\Local\Microsoft\Windows\WER\ReportArchive
\AppCrash_generatortestbed_4fa7dff09a9e893eb675f488392571ced4ac8_04ef1100
Analysis symbol:
Rechecking for solution: 0
Report Id: cd55060c-271f-11df-b6ff-001e52eefb8e
Report Status: 1
I've checked those directories, and the first doesn't exist, while the second contains a wer file that just lists the loaded dlls. 我检查了那些目录,第一个目录不存在,而第二个包含一个只列出加载的dll的wer文件。
I could install a development environment on my test machine, but then it fails to be a test machine and I'm back to square one. 我可以在我的测试机器上安装一个开发环境,但是它无法成为一台测试机器而我又回到原点。 I don't get this error with a dev environment installed, so I'm at a loss about how to get a verbose, useful error message. 我没有在安装了开发环境时遇到此错误,因此我对如何获得详细,有用的错误消息感到茫然。
EDIT: building off of @Alastair Pitts' comment below, here's how I filled out the exception handling: 编辑:建立@Alastair Pitts的评论如下,这是我填写异常处理的方式:
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
Exception theException = e.Exception;
string theErrorPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\GeneratorTestbedError.txt";
using (System.IO.TextWriter theTextWriter = new System.IO.StreamWriter(theErrorPath, true)){
DateTime theNow = DateTime.Now;
theTextWriter.WriteLine("The error time: " + theNow.ToShortDateString() + " " + theNow.ToShortTimeString());
while (theException != null) {
theTextWriter.WriteLine("Exception: " + theException.ToString());
theException = theException.InnerException;
}
}
MessageBox.Show("The program crashed. A stack trace can be found at:\n" + theErrorPath);
e.Handled = true;
Application.Current.Shutdown();
}
Hopefully, I'll get what I need this way. 希望我能以这种方式得到我需要的东西。 Thanks for the help! 谢谢您的帮助!
The procedure I would use is to handle the UnhandledException
event in the app domain. 我将使用的过程是在app域中处理UnhandledException
事件 。
Once you have done that, you have a number of options. 完成后,您有很多选择。 Logging the exception to a file, serialising it for later inspection, showing a dialog box with the exception message. 将异常记录到文件中,将其序列化以供以后检查,显示带有异常消息的对话框。
EDIT: XamlParseException
's occur when your main window is being created. 编辑:在创建主窗口时发生XamlParseException
。 This means that the constructor of that window is being called. 这意味着正在调用该窗口的构造函数。 If you perform any logic in that constructor, any resulting exceptions will throw a XamlParseException
. 如果在该构造函数中执行任何逻辑,则任何生成的异常都将抛出XamlParseException
。 It is my understanding that the UnhandledException
handler will still catch this exception. 据我所知, UnhandledException
处理程序仍将捕获此异常。
To hook up the UnhandledException
event in WPF, add the event hookup in your app.xaml 要在WPF中连接UnhandledException
事件,请在app.xaml中添加事件连接
<Application
x:Class="DispatcherUnhandledExceptionSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="App_DispatcherUnhandledException" />
which then adds a method in your app.cs 然后在app.cs中添加一个方法
public partial class App : Application
{
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Process unhandled exception do stuff below
// Prevent default unhandled exception processing
e.Handled = true;
}
}
In addition to having an unhandled exception event handler that logs the stack trace, it is also useful to look at the crash dumps being generated on the repro machine. 除了具有记录堆栈跟踪的未处理异常事件处理程序之外,查看在repro计算机上生成的故障转储也很有用。 You mentioned it's Windows 7, so you can look for corresponding crash dumps in the following ways: 您提到它是Windows 7,因此您可以通过以下方式查找相应的故障转储:
Control Panel -> All Control Panel Items -> Action Center -> "View problems to report" link underneath the Maintenance/"Check for solutions to unreported problems" area. 控制面板 - >所有控制面板项目 - >操作中心 - >“查看要报告的问题”链接位于维护/“检查未报告问题的解决方案”区域下方。 This brings up a list of applications that have crashed and you can drill down into them to find the dump files and information. 这将显示已崩溃的应用程序列表,您可以深入查看它们以查找转储文件和信息。 Look for the "View a temporary copy of these files" link in the bottom left of the problem technical details. 查找问题技术详细信息左下角的“查看这些文件的临时副本”链接。 This will extract the dump files and show them to you in an explorer window. 这将提取转储文件并在资源管理器窗口中显示给您。
cd /d %ProgramData%\\Microsoft\\Windows\\WER. cd / d%ProgramData%\\ Microsoft \\ Windows \\ WER。
WER seems to keep it's crash dumps underneath that directory, so what I do is a dir /s/b for part of the app name that I know will be there. WER似乎保留了该目录下面的崩溃转储,所以我所做的是一个dir / s / b,我知道将在那里使用部分应用程序名称。 For example, I made a on purpose crashy app that i called crashyapp.exe: 例如,我制作了一个故意崩溃的应用程序,我称之为crashyapp.exe:
C:\ProgramData\Microsoft\Windows\WER>dir /s/b *crashy* C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_crashyapp.exe_56f8d710d72e31d822d6b895c5c43a18d34acfa1_cab_2823e614That directory contains the hdmp and mdmp files for the crash. 该目录包含崩溃的hdmp和mdmp文件。 Time to get a debugger attached! 是时候连接调试器了!
Exception logging (as in the answer from Alastair Pitts) would be a help in zeroing in on the source of the error. 异常记录(如Alastair Pitts的答案)将有助于归零错误源。
The presence of XamlParse on line P9 suggests that there may be a problem initializing a control or window from the XAML description. 在P9行上存在XamlParse表明从XAML描述初始化控件或窗口可能存在问题。 There may be an assembly referenced by the XAML which was not found on the target computer, or did not match the signature in the XAML. 可能存在由XAML引用的程序集,该程序集在目标计算机上找不到,或者与XAML中的签名不匹配。
Edit: 编辑:
XAML parsing occurs in InitializeComponent() which is called in the constructor of the window or control XAML解析发生在InitializeComponent()中,它在窗口或控件的构造函数中调用
In addition to Alistair's answer, it's the InnerException that gave me the clues I was looking for: 除了Alistair的答案之外,还有InnerException给了我寻找的线索:
public partial class App : Application {
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
Exception ex = e.Exception;
Exception ex_inner = ex.InnerException;
string msg = ex.Message + "\n\n" + ex.StackTrace + "\n\n" +
"Inner Exception:\n" + ex_inner.Message + "\n\n" + ex_inner.StackTrace + "\n\n" +
Utils.RegistryVersion();
MessageBox.Show(msg, "Drop Print Batch Application Halted!", MessageBoxButton.OK);
Utils.MailReport(msg);
e.Handled = true;
Application.Current.Shutdown();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.