Skip to content

Instantly share code, notes, and snippets.

@vlkam
Last active January 21, 2019 15:06
Show Gist options
  • Save vlkam/807151a99872240918a1b3351db8cb56 to your computer and use it in GitHub Desktop.
Save vlkam/807151a99872240918a1b3351db8cb56 to your computer and use it in GitHub Desktop.
Sends custom crash report into HockeyApp
// 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);
}
@asierpn
Copy link

asierpn commented Nov 21, 2017

@vlkam your solution is working for me. Thanks.

@MrClockOff
Copy link

Nice one! Worked for me! (y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment