Last active
September 16, 2015 16:07
-
-
Save Crisfole/676ad406612f8421f9b5 to your computer and use it in GitHub Desktop.
Using Valetude.Rollbar with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
using Valetude.Rollbar; | |
namespace MyCompany.Whatever | |
{ | |
internal static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
private static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
// The Following Three Lines of Code | |
Application.ThreadException += new ThreadExceptionEventHandler(this.HandleUIException); | |
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); | |
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.HandleException); | |
Application.Run(new MyForm()); | |
} | |
private static void HandleUIException(object sender, ThreadExceptionEventArgs t) | |
{ | |
HandleException(a.Exception, sender, ErrorLevel.Error); | |
} | |
private static void HandleException(object sender, UnhandledExceptionEventArgs a) | |
{ | |
HandleException(a.ExceptionObject, sender, a.IsTerminating ? ErrorLevel.Critical : ErrorLevel.Error); | |
} | |
private static void HandleException(Exception e, object sender, ErrorLevel level) | |
{ | |
var payload = new RollbarPayload(MYGLOBAL_OR_INJECTED_ACCESS_TOKEN, new RollbarData("production", new RollbarBody(e)) { Level = level }); | |
var response = Rollbar.Send(payload); | |
// You can inspect response for more info if desired | |
} | |
} | |
public static class Rollbar { | |
private const string RollbarApiEndpoint = "https://api.rollbar.com/api/1/item/"; | |
private const string ContentType = "application/json"; | |
public static RollbarResponse Send(RollbarPayload payload) { | |
using (var client = new WebClient()) { | |
client.Headers[HttpRequestHeader.ContentType] = ContentType; | |
try { | |
return new RollbarResponse { | |
Message = client.UploadString(new Uri(RollbarApiEndpoint), "POST", payload.ToJson()), | |
StatusCode = RollbarResponseCode.Success, | |
}; | |
} | |
catch (WebException exception) { | |
return new RollbarResponse { | |
Message = exception.Message, | |
StatusCode = ToRollbarStatus(exception.Status), | |
}; | |
} | |
} | |
} | |
private static RollbarResponseCode ToRollbarStatus(WebExceptionStatus status) { | |
switch ((int) status) { | |
case 200: | |
return RollbarResponseCode.Success; | |
case 400: | |
return RollbarResponseCode.BadRequest; | |
case 401: | |
return RollbarResponseCode.Unauthorized; | |
case 403: | |
return RollbarResponseCode.AccessDenied; | |
case 413: | |
return RollbarResponseCode.RequestTooLarge; | |
case 422: | |
return RollbarResponseCode.UnprocessablePayload; | |
case 429: | |
return RollbarResponseCode.TooManyRequests; | |
case 500: | |
return RollbarResponseCode.InternalServerError; | |
default: | |
throw new ArgumentException("Invalid Status returned from Rollbar", "status"); | |
} | |
} | |
} | |
public class RollbarResponse { | |
public RollbarResponseCode StatusCode { get; set; } | |
public string Message { get; set; } | |
} | |
public enum RollbarResponseCode { | |
Success = 200, | |
BadRequest = 400, | |
Unauthorized = 401, | |
AccessDenied = 403, | |
RequestTooLarge = 413, | |
UnprocessablePayload = 422, | |
TooManyRequests = 429, | |
InternalServerError = 500, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment