Search code examples
c#wpfwindowsexceptionhockeyapp

Send handled Exception report using HockeyApp(Windows)


I am trying to Send Error reports with hockeyapp without having to let the whole app crash and burn. I dont think the HockeyApp.WPF library has this capability, so I started to mess around with implementing my own CrashHandler.

This quickly got confusing and very hackey. Does anyone have any code examples for this? At my current rate I will end up reproducing half of the HockeyApp Library, so I would appreciate some help.

I am not posting my code because I don't think it will help and its too much.

Edit: now I will post a shortened version of code that doesnt seem to work:

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

Solution

  • I was able to make a post after formatting the logs as described in the documentation for the crashes/upload endpoint(http://support.hockeyapp.net/kb/api/api-crashes#-u-post-api-2-apps-app_id-crashes-upload-u-).

    Although I ended up hitting "crashes/", which from my understanding is different from crashes/upload(Therefore this is a solution that is hitting an undocumented endpoint).

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