public void NotifyAsync_ShouldSetStatusToIgnoredIfEnvironmentIsIgnored() { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6", Environment = "test", IgnoreEnvironments = new List <string> { "test" } }; using (var requestHandler = new FakeHttpRequestHandler()) { var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler); var resetEvent = new AutoResetEvent(false); AirbrakeResponse airbrakeResponse = null; notifier.NotifyCompleted += (sender, eventArgs) => { airbrakeResponse = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(new Exception()); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); Assert.True(airbrakeResponse.Status == RequestStatus.Ignored); } }
public void NotifyAsync_ShouldSetStatusToIgnoredIfNoticeIsNullAfterApplyingFilters() { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6" }; using (var requestHandler = new FakeHttpRequestHandler()) { var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler); notifier.AddFilter(notice => null); #if NET35 var resetEvent = new AutoResetEvent(false); AirbrakeResponse airbrakeResponse = null; notifier.NotifyCompleted += (sender, eventArgs) => { airbrakeResponse = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(new Exception()); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); #else var airbrakeResponse = notifier.NotifyAsync(new Exception()).Result; #endif Assert.True(airbrakeResponse.Status == RequestStatus.Ignored); } }
public void NotifyAsync_ShouldInitializeHttpContextOnlyIfProvided(bool isHttpContextProvided) { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6" }; using (var requestHandler = new FakeHttpRequestHandler()) { requestHandler.HttpResponse.StatusCode = HttpStatusCode.Created; requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}"; var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler); FakeHttpContext context = null; if (isHttpContextProvided) { context = new FakeHttpContext { UserAgent = "test" } } ; #if NET35 var resetEvent = new AutoResetEvent(false); AirbrakeResponse airbrakeResponse = null; notifier.NotifyCompleted += (sender, eventArgs) => { airbrakeResponse = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(new Exception(), context); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); #else var airbrakeResponse = notifier.NotifyAsync(new Exception(), context).Result; #endif var notice = NoticeBuilder.FromJsonString(requestHandler.HttpRequest.GetRequestStreamContent()); Assert.True(airbrakeResponse.Status == RequestStatus.Success); if (isHttpContextProvided) { Assert.True(notice.Context != null); } else { Assert.True(notice.Context == null || string.IsNullOrEmpty(notice.Context.UserAgent)); } } }
public void Log_ShouldNotLogExceptionIfEmpty() { var logFile = Guid.NewGuid() + ".log"; var logger = new FileLogger(logFile); AirbrakeResponse response = null; logger.Log(response); Assert.True(!File.Exists(logger.LogFile)); File.Delete(logger.LogFile); }
/// <summary> /// Logs response from the Airbrake endpoint. /// </summary> public void Log(AirbrakeResponse response) { if (response == null) { return; } using (var file = new FileStream(logFile, FileMode.Append, FileAccess.Write)) { lock (locker) { using (var writer = new StreamWriter(file, Encoding.UTF8)) writer.WriteLine("[{0}]\t{1}\t{2}\t{3}", DateTime.Now, response.Status, response.Id, response.Url); } } }
public void Log_ShouldLogResponseIfNotEmpty() { var logFile = Guid.NewGuid() + ".log"; var logger = new FileLogger(logFile); var response = new AirbrakeResponse { Id = "0005488e-8947-223e-90ca-16fec30b6d72", Url = "https://airbrake.io/locate/0005488e-8947-223e-90ca-16fec30b6d72", Status = RequestStatus.Success }; logger.Log(response); Assert.True(File.Exists(logger.LogFile)); Assert.True(!string.IsNullOrEmpty(File.ReadAllText(logger.LogFile))); File.Delete(logger.LogFile); }
public void NotifyAsync_ShouldUpdateNoticeAfterApplyingFilters() { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6" }; using (var requestHandler = new FakeHttpRequestHandler()) { requestHandler.HttpResponse.StatusCode = HttpStatusCode.Created; requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}"; var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler); notifier.AddFilter(notice => { notice.Context.Action = "modified action"; return(notice); }); #if NET35 var resetEvent = new AutoResetEvent(false); AirbrakeResponse airbrakeResponse = null; notifier.NotifyCompleted += (sender, eventArgs) => { airbrakeResponse = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(new Exception()); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); #else var airbrakeResponse = notifier.NotifyAsync(new Exception()).Result; #endif var actualNotice = NoticeBuilder.FromJsonString(requestHandler.HttpRequest.GetRequestStreamContent()); Assert.True(airbrakeResponse.Status == RequestStatus.Success); Assert.NotNull(actualNotice.Context); Assert.True(actualNotice.Context.Action == "modified action"); } }
public void NotifyAsync_ShouldSetRequestStatusToSuccessOnlyIfStatusCodeCreated(bool isStatusCodeCreated) { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6" }; using (var requestHandler = new FakeHttpRequestHandler()) { requestHandler.HttpResponse.StatusCode = isStatusCodeCreated ? HttpStatusCode.Created : HttpStatusCode.BadRequest; requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}"; var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler); #if NET35 var resetEvent = new AutoResetEvent(false); AirbrakeResponse airbrakeResponse = null; notifier.NotifyCompleted += (sender, eventArgs) => { airbrakeResponse = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(new Exception()); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); #else var airbrakeResponse = notifier.NotifyAsync(new Exception()).Result; #endif if (isStatusCodeCreated) { Assert.True(airbrakeResponse.Status == RequestStatus.Success); } else { Assert.True(airbrakeResponse.Status == RequestStatus.RequestError); } } }
public void LogResponse_ShouldLogIfResponseNotEmpty() { var logFile = Guid.NewGuid() + ".log"; var response = new AirbrakeResponse { Id = "0005488e-8947-223e-90ca-16fec30b6d72", Url = "https://api.airbrake.io/locate/0005488e-8947-223e-90ca-16fec30b6d72", Status = RequestStatus.Success }; try { Utils.LogResponse(logFile, response); Assert.True(File.Exists(logFile)); Assert.True(!string.IsNullOrEmpty(File.ReadAllText(logFile))); } finally { File.Delete(logFile); } }
public void NotifyAsync_1xRequest() { var config = new AirbrakeConfig { ProjectId = "127348", ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6", Host = HttpServer.Host }; var httpRequestHandler = new HttpRequestHandler(config.ProjectId, config.ProjectKey, config.Host); var notifier = new AirbrakeNotifier(config, null, httpRequestHandler); try { var zero = 0; var result = 1 / zero; } catch (Exception ex) { #if NET35 var resetEvent = new AutoResetEvent(false); AirbrakeResponse response = null; notifier.NotifyCompleted += (sender, eventArgs) => { response = eventArgs.Result; resetEvent.Set(); }; notifier.NotifyAsync(ex); Assert.Equal(resetEvent.WaitOne(2000), true); resetEvent.Close(); #else var response = notifier.NotifyAsync(ex).Result; #endif Assert.True(response.Status.Equals(RequestStatus.Success)); } }
public void Log(AirbrakeResponse response) { LoggedResponses.Add(response); }