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);
}
@vlkam
Copy link
Author

vlkam commented Nov 21, 2017

Crash log for iOS

Package: com.fsjunior.junior
Version Code: 1.0.51
Version Name: iOS
Android: 10.3.3
Android Build: iOS
Manufacturer: Apple
Model: iPhone 6S
Thread: Thread-unknown
CrashReporter Key: 9de38b4f-ac81-4908-b081-83d7b1fd9639
Start Date: Fri, 17 Nov 2017 03:04:14 GMT 2017
Date: Fri, 17 Nov 2017 03:04:14 GMT 2017
Format: Xamarin
Caught: true

System.Exception: Key NewsModel4406 already exists in the dictionary
at Realms.WeakCache.Add (System.String key, Realms.RealmObject value) <0x100fa4c70 + 0x00090> in <e72842efb5114c67a23b3befbbd43568#9025f1ceeb0870726cc46b222d796511>:0
at Realms.Realm.AddInternal (Realms.RealmObject obj, System.Type objectType, System.Boolean update) <0x100fa6c50 + 0x0038b> in <e72842efb5114c67a23b3befbbd43568#9025f1ceeb0870726cc46b222d796511>:0
at Realms.Realm.Add[T] (T obj, System.Boolean update) <0x100fa6b90 + 0x0003b> in <e72842efb5114c67a23b3befbbd43568#9025f1ceeb0870726cc46b222d796511>:0
at CoreXF.DefaultDataAccessManager1[T].AddRange (System.Collections.Generic.IEnumerable1[T] range, System.Boolean update) <0x100912bc0 + 0x000e3> in <0069b4424c1f487c98987f9fe5ac4f68#9025f1ceeb0870726cc46b222d796511>:0
at JCabinet.SyncManager+SyncNewsManager+d__3.MoveNext () <0x100347f30 + 0x009f3> in <95bf17ad64f94bd5a1a4004966e84ec9#9025f1ceeb0870726cc46b222d796511>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x1001f6eb0 + 0x00028> in <6314851f133e4e74a2e96356deaa0c6c#9025f1ceeb0870726cc46b222d796511>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1001fa080 + 0x000d3> in <6314851f133e4e74a2e96356deaa0c6c#9025f1ceeb0870726cc46b222d796511>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1001f9fe0 + 0x0007f> in <6314851f133e4e74a2e96356deaa0c6c#9025f1ceeb0870726cc46b222d796511>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) <0x1001f9f80 + 0x00047> in <6314851f133e4e74a2e96356deaa0c6c#9025f1ceeb0870726cc46b222d796511>:0
at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () <0x1002ad290 + 0x0001b> in <6314851f133e4e74a2e96356deaa0c6c#9025f1ceeb0870726cc46b222d796511>:0 at CoreXF.HttpRequestManager1+d__7[T].MoveNext () <0x1008fe0f0 + 0x00533> in <0069b4424c1f487c98987f9fe5ac4f68#9025f1ceeb0870726cc46b222d796511>:0

@danilkurkin1
Copy link

@vlkam Thanks man for your work on this - but in my case I cannot make it work =(
I receive 201 from HockeyApp with

id 0 delay 0 status 0

as feedback.

Where you can look for those reports on HockeyApp ? thx

@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