Sends custom crash report into HockeyApp

vlkam
2017-11-20T10:26:19Z

gistfile1.txt

|
| // https://support.hockeyapp.net/kb/api/api-crashes
| public static async Task<bool> SendHandledException(CaughtExceptionModel model)
| {
| string version = DeviceInfo.AppVersion;
| var arr = version.Split('.');
| string short_ver = arr[arr.Length - 1];
| var deviceInfo = Plugin.DeviceInfo.CrossDeviceInfo.Current;
|
| StringBuilder sb = new StringBuilder(12000);
| switch (Device.RuntimePlatform)
| {
| case Device.Android:
| sb
| .Append("Package: ").AppendLine(AppConfig.PackageId)
| .Append("Version Code: ").AppendLine(short_ver)
| .Append("Version Name: ").AppendLine(version)
| .Append("Android: ").AppendLine(DeviceInfo.OsVersion)
| .Append("Android Build: ").AppendLine(DeviceInfo.OsBuild)
| .Append("Manufacturer: ").AppendLine(DeviceInfo.Manufacturer)
| .Append("Model: ").Append(deviceInfo.Model).Append(" ").AppendLine(deviceInfo.Idiom.ToString())
| .Append("Thread: Thread-unknown").AppendLine()
| .Append("CrashReporter Key: ").AppendLine(AppConfig.HockeyAppCrashReporterKey)
| .Append($"Start Date: {model.DateTime.ToUniversalTime().ToString("r")} {model.DateTime.ToString("yyyy")}").AppendLine()
| .Append($"Date: {model.DateTime.ToUniversalTime().ToString("r")} {model.DateTime.ToString("yyyy")}").AppendLine()
| .Append("Format: Xamarin").AppendLine()
| .AppendLine("Caught: true")
| .Append("").AppendLine()
| .Append(exception.ToString()).AppendLine();
| break;
|
| case Device.iOS:
| sb
| .Append("Package: ").AppendLine(AppConfig.PackageId)
| .Append("Version Code: ").AppendLine(DeviceInfo.AppVersion)
| .Append("Version Name: ").AppendLine("iOS")
| .Append("Android: ").AppendLine(DeviceInfo.OsVersion)
| .Append("Android Build: ").AppendLine(DeviceInfo.OsBuild)
| .Append("Manufacturer: ").AppendLine("Apple")
| .Append("Model: ").AppendLine(DeviceInfo.DeviceName)
| .Append("Thread: Thread-unknown").AppendLine()
| .Append("CrashReporter Key: ").AppendLine(UserSettings.AppUUID)
| .Append($"Start Date: {model.DateTime.ToUniversalTime().ToString("r")} {model.DateTime.ToString("yyyy")}").AppendLine()
| .Append($"Date: {model.DateTime.ToUniversalTime().ToString("r")} {model.DateTime.ToString("yyyy")}").AppendLine()
| .Append("Format: Xamarin").AppendLine()
| .AppendLine("Caught: true")
| .Append("").AppendLine()
| .Append(exception.ToString()).AppendLine();
| break;
| }
|
| HttpClient client = new HttpClient();
| var content1 = new MultipartFormDataContent();
|
| var stringContent = new StringContent(sb.ToString());
|
| stringContent.Headers.Add("Content-Disposition", "form-data; name=\"log\"; filename=\"log.txt\"");
| RemoveQuotes(stringContent);
|
| content1.Add(stringContent);
| RemoveQuotes(content1);
|
| try
| {
| using (var message = await client.PostAsync($"https://rink.hockeyapp.net/api/2/apps/{AppConfig.HockeyAppId}/crashes/upload?userID={WebUtility.UrlEncode(UserSettings.LastLogin)}", content1))
| {
| return message.StatusCode == System.Net.HttpStatusCode.Created;
| }
| }
| catch (Exception ex)
| {
| Debug.WriteLine(ex);
| }
| return false;
| }
|
| static void RemoveQuotes(HttpContent content)
| {
| var contentTypeString = content.Headers.ContentType.ToString().Replace("\"", "");
| content.Headers.Remove("Content-Type");
| content.Headers.TryAddWithoutValidation("Content-Type", contentTypeString);
| }