是否存在用于此目的的现有.NET库/项目?或者,是否这个方法听起来正确,只需在应用程序入口点捕获所有异常?
是否存在用于此目的的现有.NET库/项目?或者,是否这个方法听起来正确,只需在应用程序入口点捕获所有异常?
请参考.NET中所有可用的日志框架,其中任何一个都应该提供电子邮件通知。
我认为最佳实践是拥有一个顶级异常处理程序,它可以收集和记录未捕获异常的数据。正如Meeh在他对你的问题的评论中提到的那样,你需要为应用程序中的每个线程都创建一个异常处理程序。
这篇来自Joel的旧文章介绍了FogBugz中的错误报告功能,也许这会给你一些更多的想法。(我记得我在他的博客上读过它,但我找到的只有这个来自FogBugz文档的页面)。
免责声明:我是这个产品的开发人员。
使用由我们公司(PreEmptive Solutions)编写的产品(Runtime Intelligence),您可以注入不仅错误报告,还可以跟踪用户何时使用您的应用程序以及他们使用哪些功能,而只需进行最少的编码。
使用Dotfuscator执行代码注入(或IL编织),我们将新代码插入到您的应用程序二进制文件中,将使用数据发送回我们设施托管的服务器(或可选地发送到任意其他URL)。如果您将数据发送给我们,我们会为您提供许多强大的分析工具和使用情况报告。
Visual Studio 2010中将包含此功能的基本版本,以及访问免费数据报告门户网站(但没有SLA、数据保留保证或数据隐私)。
将任意数据与使用信息一起发送的能力仅限于商业产品,但您可以联系PreEmptive Solutions获取完全功能的免费时间限制评估版本。
您可以使用以下示例代码完成错误报告:
public partial class app : Application {
// field to temporarily store exception data
private Exception exp;
void AppStartup(object sender, StartupEventArgs args) {
// add a handler to catch any unhandled exceptions
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(ErrorHandler);
Window1 mainWindow = new Window1();
mainWindow.ShowDialog();
}
// this will prompt the user if they want to report the exception
void ErrorHandler(object sender, DispatcherUnhandledExceptionEventArgs e) {
this.exp = e.Exception;
if (MessageBox.Show("A " + exp.Message + " exception happened, should I report it?", "Error Occurrend", MessageBoxButton.YesNo) == MessageBoxResult.Yes) {
ErrorHappened();
e.Handled = true;
}
}
// this method is called by the above ErrorHandler method and when run through Dotfuscator additional code will be injected into this method that will send a usage data message back to the server and the data in the dictionary (which will be exception data) returned by the ErrorData method will be included into the message and be stored and reported on the server
[Feature("Exception", EventType = FeatureEventTypes.Tick, ExtendedKeySourceElement = SourceElements.Method, ExtendedKeySourceName = "ErrorData")]
private void ErrorHappened() {
// This is here as a placeholder for the exception feature attribute which will exit the application when executed
AppShutdown(true);
}
// this method takes the exception data from the exp field and returns it as a dictionary of name/value pairs
public Dictionary<string, string> ErrorData() {
var retval = new Dictionary<string,string>();
if (null != exp) {
retval.Add("Error Message",exp.Message);
retval.Add("Stack Trace",exp.StackTrace);
}
return retval;
}
}
我在开源项目Exceptionless上工作,该项目有客户端库可以实现这一点!我们捕获所有未处理的异常并将它们发送到一个rest api,然后实时处理。
使用与Windows相同的工具:Windows错误报告(WER)。它将进行崩溃分析并将您的崩溃分组,以便您可以了解哪些是最常见的。无需进行代码更改,它在进程级别上运行。它将记录您无法捕获的异常失败。
当然,这一切都是基于您正在运行Windows系统。
我修改了Jeff的用户友好的异常处理程序,并将其更新为.NET 2.0/3.5版本,使用效果非常好。如果一个异常一路上未被处理,程序会自动截屏并发送电子邮件,同时附带详细的堆栈跟踪信息给开发团队。