我正在尝试使用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;
    }

最佳答案

如崩溃/上传端点(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");

            }

        }

10-06 11:24