.NET“向[我]发送错误报告”

时间:2009-07-25 20:23:23

标签: .net exception error-handling error-reporting

如果异常一直传播到我的应用程序的顶部(当然,这将永远不会发生),我想提供在程序崩溃之前向我发送错误报告的选项。我想到的是在一个try-catch中包装Main函数中的所有东西,以及一个将堆栈跟踪和其他信息发送到我的服务器的小类。这听起来很简单,但有了这样的话,我确信我还没有充分考虑过障碍(例如安全性,面向未来)。

是否存在用于此目的的现有.NET库/项目?或者,这听起来像是正确的方法,只是在应用程序的入口点捕获所有Exception?

7 个答案:

答案 0 :(得分:4)

请参阅.NET中所有可用日志框架的this question,其中任何一个都应提供电子邮件通知。

我认为最佳做法是拥有一个顶级异常处理程序,用于收集和记录未捕获异常的数据。正如Meeh在他对你的问题的评论中提到的那样,你的应用程序中的每个帖子都需要一个。

FogBugz中的错误报告功能有一个old article from Joel,也许会给你更多的想法。 (我想我是在他的博客上看到的,但我能找到的就是这个页面形成了FogBugz文档。)

答案 1 :(得分:2)

当您捕获并记录异常时,您可以使用Logging Application block中的Enterprise Library轻松发送电子邮件。以下是有关如何create a last chance exception handler的一些信息。我假设你有一个Winform应用程序,但你的问题并不清楚。

答案 2 :(得分:1)

使用Windows使用的相同工具:Windows Error Reporting(WER)。它将进行崩溃分析并将您的崩溃文本化,以便您了解哪些是最常见的。无需更改代码,它在流程级别工作。它将记录您无法捕获的异常中的失败。

当然,所有假设你都在Windows上运行。

答案 3 :(得分:1)

我的标准免责声明:我是此产品的开发人员。

使用我工作的公司(PreEmptive Soltutions)编写的产品(运行时智能),您不仅可以注入错误报告,还可以跟踪用户何时使用您的应用程序以及使用最少编码的功能。

使用Dotfuscator执行代码注入(或IL编织),我们将新代码插入到您的应用程序二进制文件中,这些二进制文件将使用数据发送回我们工厂托管的服务器(或者可选地发送到任何其他任意URL)。如果您将数据发送给我们,我们会为您提供许多强大的分析工具和使用情况报告。

此功能的基本版本将包含在Visual Studio 2010中,以及访问免费数据报告门户(但没有SLA,数据保留保证或数据隐私)。

将任意数据与使用信息一起发送的能力仅限于商业产品,但您可以联系PreEmptive Soltutions获取功能齐全的免费时间限制评估版本。

您可以使用以下示例代码完成错误报告:

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;
    }
}

答案 4 :(得分:1)

我修改了Jeff的User-Friendly Exception Handler并为.NET 2.0 / 3.5更新了它,并且我很幸运。如果异常使堆栈处于未处理状态,则会截取屏幕截图并通过电子邮件将详细的堆栈跟踪发送给开发团队。

答案 5 :(得分:1)

我在开源项目上工作Exceptionless,它有客户端库就可以做到这一点!我们捕获所有未处理的异常并将它们发送到休息api,然后实时处理它们。

https://github.com/exceptionless/Exceptionless

答案 6 :(得分:0)

如果您正在使用ASP.NET应用程序,则还可以使用ASP.NET运行状况监视功能。涉及在配置文件中添加几行并且您已关闭。 http://msdn.microsoft.com/en-us/library/bb398933.aspx