From c4882062e7e7072784eba7e67d4448d006be1dc9 Mon Sep 17 00:00:00 2001 From: Julien Binet Date: Thu, 24 Nov 2016 14:43:40 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20m=C3=A9thode=20synchrone=20necessa?= =?UTF-8?q?ire=20pour=20pouvoir=20envoyer=20la=20requ=C3=AAte=20lors=20de?= =?UTF-8?q?=20r=C3=A9ception=20d=E2=80=99exception=20non=20captur=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RollbarDotNet/IRollbar.cs | 8 +++- RollbarDotNet/RollbarClient.cs | 26 ++++++++++- RollbarDotNet/RollbarImplementation.cs | 60 +++++++++++++++++++++----- 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/RollbarDotNet/IRollbar.cs b/RollbarDotNet/IRollbar.cs index 50fc1ea..61b455e 100644 --- a/RollbarDotNet/IRollbar.cs +++ b/RollbarDotNet/IRollbar.cs @@ -8,8 +8,12 @@ public interface IRollbar { void PersonData(Func personFunc); - Task Report(System.Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); + Task ReportAsync(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); - Task Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); + Task ReportAsync(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); + + Guid? Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); + + Guid? Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null); } } diff --git a/RollbarDotNet/RollbarClient.cs b/RollbarDotNet/RollbarClient.cs index 8fd6eb1..7cd8fab 100644 --- a/RollbarDotNet/RollbarClient.cs +++ b/RollbarDotNet/RollbarClient.cs @@ -19,6 +19,12 @@ public async Task PostItemAsync(Payload payload) { var stringResult = await SendPostAsync("item/", payload); return ParseResponse(stringResult); + } + + public Guid PostItem(Payload payload) + { + var stringResult = SendPost("item/", payload); + return ParseResponse(stringResult); } static Guid ParseResponse(string stringResult) @@ -54,7 +60,7 @@ async Task SendPostAsync(string url, T payload) try { var httpClient = new HttpClient(); - httpClient.BaseAddress = (new Uri(Config.EndPoint)); + httpClient.BaseAddress = (new Uri(Config.EndPoint)); var result = await httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(payload))); return await result.Content.ReadAsStringAsync(); @@ -66,5 +72,23 @@ async Task SendPostAsync(string url, T payload) return string.Empty; } } + + string SendPost(string url, T payload) + { + try + { + var httpClient = new HttpClient(); + httpClient.BaseAddress = (new Uri(Config.EndPoint)); + + var result = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(payload))).Result; + return result.Content.ReadAsStringAsync().Result; + } + catch (Exception ex) + { + Debug.WriteLine("[RollbarDotNet] The request sent to the api failed."); + Debug.WriteLine(ex); + return string.Empty; + } + } } } diff --git a/RollbarDotNet/RollbarImplementation.cs b/RollbarDotNet/RollbarImplementation.cs index 48b6325..ad8b0ba 100644 --- a/RollbarDotNet/RollbarImplementation.cs +++ b/RollbarDotNet/RollbarImplementation.cs @@ -22,24 +22,49 @@ public void PersonData(Func personFunc) _personFunc = personFunc; } - public async Task Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) + public async Task ReportAsync(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) { - return await SendBody(new Body(e), level, custom); + if (!IsCanSend) + return null; + + var payload = BuildPayload(new Body(e), level, custom); + return await SendBodyAsync(payload); } - public async Task Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) + public async Task ReportAsync(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) { - return await SendBody(new Body(new Message(message)), level, custom); + if (!IsCanSend) + return null; + + var payload = BuildPayload(new Body(new Message(message)), level, custom); + return await SendBodyAsync(payload); } - static async Task SendBody(Body body, ErrorLevel? level, IDictionary custom) + public Guid? Report(Exception e, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) { - if (string.IsNullOrWhiteSpace(_config.AccessToken) || _config.Enabled == false) + if (!IsCanSend) return null; - var guid = Guid.NewGuid(); + var payload = BuildPayload(new Body(e), level, custom); + return SendBody(payload); + } - var client = new RollbarClient(_config); + public Guid? Report(string message, ErrorLevel? level = ErrorLevel.Error, IDictionary custom = null) + { + if (!IsCanSend) + return null; + + var payload = BuildPayload(new Body(new Message(message)), level, custom); + return SendBody(payload); + } + + static bool IsCanSend + { + get { return !string.IsNullOrWhiteSpace(_config.AccessToken) && _config.Enabled == true; } + } + + static Payload BuildPayload(Body body, ErrorLevel? level, IDictionary custom) + { var data = new Data(_config.Environment, body) { Custom = custom, @@ -47,13 +72,26 @@ public void PersonData(Func personFunc) }; var payload = new Payload(_config.AccessToken, data); - payload.Data.GuidUuid = guid; + payload.Data.GuidUuid = Guid.NewGuid(); payload.Data.Person = _personFunc?.Invoke(); - _config.Transform?.Invoke(payload); + return payload; + } + + static async Task SendBodyAsync(Payload payload) + { + var client = new RollbarClient(_config); await client.PostItemAsync(payload); - return guid; + return payload.Data.GuidUuid; + } + + static Guid? SendBody(Payload payload) + { + var client = new RollbarClient(_config); + client.PostItem(payload); + + return payload.Data.GuidUuid; } } }