Skip to content

Instantly share code, notes, and snippets.

@zarakay
Last active April 12, 2016 22:23
Show Gist options
  • Save zarakay/ad987a25cecedf86e285597515b2fa70 to your computer and use it in GitHub Desktop.
Save zarakay/ad987a25cecedf86e285597515b2fa70 to your computer and use it in GitHub Desktop.
Bugsnag.NET code
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using OzGuildAPI.Authentication;
using OzGuildAPI.Models.Responses;
using BsNet = Bugsnag.PCL;
namespace OzGuild.Common
{
public class BugsnagHelper
{
public static BugsnagHelper Instance { get; } = new BugsnagHelper();
private static UserResponse CurrentUser => OAuthClient.CurrentUser;
private BugsnagHelper()
{
BsNet.Bugsnag.ApiKey = "< APP KEY HERE >";
var v = Windows.ApplicationModel.Package.Current.Id.Version;
var version = $"{v.Major}.{v.Minor}.{v.Build}.{v.Revision}";
BsNet.Bugsnag.App = new BsNet.Request.App
{
Version = version,
ReleaseStage = "production"
};
}
public void ReportError(Exception e)
{
ReportEvent(BsNet.Bugsnag.Error, e);
}
public void ReportWarning(Exception e)
{
ReportEvent(BsNet.Bugsnag.Warning, e);
}
public void ReportInfo(Exception e)
{
ReportEvent(BsNet.Bugsnag.Info, e);
}
private void ReportEvent(BsNet.Bugsnag client, Exception exception)
{
Debug.WriteLine("Error Detected Notifying Bugsnag...");
var evt = client.GetEvent(exception, GetCurrentUser(), GetMetadata());
var response = Task.Run(() => client.NotifyAsync(evt)).Result;
Debug.WriteLine("Bugnsag notify finished with response: " + response.StatusCode);
}
private static BsNet.Request.IUser GetCurrentUser()
{
if (CurrentUser != null)
{
return new BsNet.Request.User(CurrentUser.Id)
{
Email = CurrentUser.Email,
Name = CurrentUser.Name
};
}
return null;
}
private static object GetMetadata()
{
return null;
}
}
}
// Example Callsite in some method
BugsnagHelper.Instance.ReportError(new ArgumentException("Test"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment