使用HockeyApp发送处理的异常报告(Windows)

时间:2015-03-13 14:58:21

标签: c# wpf windows exception-handling hockeyapp

我正在尝试使用hockeyapp发送错误报告,而不必让整个应用程序崩溃并刻录。我不认为HockeyApp.WPF库有这个功能,所以我开始搞乱实现我自己的CrashHandler

这很快就让人感到困惑和非常hackey。有没有人有任何代码示例?以我目前的速度,我将最终复制HockeyApp图书馆的一半,所以我将不胜感激。

我没有发布我的代码,因为我认为它不会有太大的帮助。

编辑:现在我将发布一个似乎不起作用的缩短版代码:

        private static void HandleException(Exception e) {
        try {
            string crashID = Guid.NewGuid().ToString();
            String filename = String.Format("{0}{1}.log", CrashFilePrefix, crashID);

            CrashLogInformation logInfo = new CrashLogInformation() {
                PackageName = Application.Current.GetType().Namespace,
                Version = ((HockeyClient)HockeyClient.Current).VersionInfo,
                OperatingSystem = Environment.OSVersion.Platform.ToString(),
                Windows = Environment.OSVersion.Version.ToString() + Environment.OSVersion.ServicePack,
                Manufacturer = "",
                Model = ""
            };

            ICrashData crash = ((HockeyClient)HockeyClient.Current).CreateCrashData(e);
            using (FileStream stream = File.Create(Path.Combine(GetPathToHockeyCrashes(), filename))) {
                crash.Serialize(stream);
                stream.Flush();
            }
        }
        catch (Exception ex) {
            ((HockeyClient)HockeyClient.Current).HandleInternalUnhandledException(ex);
        }
    }
    private static string GetPathToHockeyCrashes() {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        if (!path.EndsWith("\\")) { path += "\\"; }
        path += "HockeyCrashes\\";
        if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }
        return path;
    }


    private struct CrashLogInformation {
        /// <summary>
        /// name of app package
        /// </summary>
        public string PackageName;
        /// <summary>
        /// version of app
        /// </summary>
        public string Version;
        /// <summary>
        /// os
        /// </summary>
        public string OperatingSystem;
        /// <summary>
        /// device manufacturer
        /// </summary>
        public string Manufacturer;
        /// <summary>
        /// device model
        /// </summary>
        public string Model;
        /// <summary>
        /// product id of app
        /// </summary>
        public string ProductID;
        /// <summary>
        /// windows phone version
        /// </summary>
        public string WindowsPhone;
        /// <summary>
        /// windows version
        /// </summary>
        public string Windows;
    }

1 个答案:

答案 0 :(得分:5)

我可以按照崩溃/上传端点(http://support.hockeyapp.net/kb/api/api-crashes#-u-post-api-2-apps-app_id-crashes-upload-u-)文档中的说明格式化日志后发帖。

虽然我最终遇到了“崩溃/”,但我的理解与崩溃/上传不同(因此这是一个触及未记录端点的解决方案)。

private static readonly string HOCKEYUPLOADURL = @"https://rink.hockeyapp.net/api/2/apps/{0}/crashes/";

     private static async Task SendDataAsync(String log, String userID, String contact, String description) {
            string rawData = "";
            rawData += "raw=" + Uri.EscapeDataString(log);
            if (userID != null) {
                rawData += "&userID=" + Uri.EscapeDataString(userID);
            }
            if (contact != null) {
                rawData += "&contact=" + Uri.EscapeDataString(contact);
            }
            if (description != null) {
                rawData += "&description=" + Uri.EscapeDataString(description);
            }
            WebRequest request = WebRequest.Create(new Uri(String.Format(HOCKEYUPLOADURL, HOCKEYAPPID)));

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            using (Stream stream = await request.GetRequestStreamAsync()) {
                byte[] byteArray = Encoding.UTF8.GetBytes(rawData);
                stream.Write(byteArray, 0, rawData.Length);
                stream.Flush();
            }

            try {
                using (WebResponse response = await request.GetResponseAsync()) { }
            }
            catch (WebException e) {
                WriteLocalLog(e, "HockeyApp SendDataAsync failed");

            }

        }