Exemple #1
0
        private async Task <Response> SendDataAsync(TData dataToSend, int retryNum)
        {
            HttpResponseMessage httpResponse;

            try
            {
                _captureSendDataAsyncCallDelegate?.Invoke(dataToSend, retryNum);

                if (ContainsNoData(dataToSend))
                {
                    return(Response.DidNotSend);
                }

                var serializedPayload = dataToSend.ToJson();

                httpResponse = await _httpHandlerImpl(serializedPayload);
            }
            catch (Exception ex)
            {
                _logger.Exception(ex.InnerException ?? ex);
                return(Response.Exception(ex.InnerException ?? ex));
            }

            switch (httpResponse.StatusCode)
            {
            //Success is any 2xx response
            case HttpStatusCode code when code >= HttpStatusCode.OK && code <= (HttpStatusCode)299:
                _logger.Debug($@"Response from New Relic ingest API: code: {httpResponse.StatusCode}");
                return(Response.Success);

            case HttpStatusCode.RequestEntityTooLarge:
                _logger.Warning($@"Response from New Relic ingest API: code: {httpResponse.StatusCode}. Response indicates payload is too large.");
                return(await RetryWithSplit(dataToSend));

            case HttpStatusCode.RequestTimeout:
                _logger.Warning($@"Response from New Relic ingest API: code: {httpResponse.StatusCode}");
                return(await RetryWithDelay(dataToSend, retryNum));

            case (HttpStatusCode)429:
                _logger.Warning($@"Response from New Relic ingest API: code: {httpResponse.StatusCode}. ");
                return(await RetryWithServerDelay(dataToSend, retryNum, httpResponse));

            //Anything else is interpreted as a failure condition.  No further attempts are made.
            default:
                _logger.Error($@"Response from New Relic ingest API: code: {httpResponse.StatusCode}");
                return(Response.Failure(httpResponse.StatusCode, httpResponse.Content?.ToString()));
            }
        }
        public void TestLogging()
        {
            var loggerFactory     = new LoggerFactory();
            var customLogProvider = new CustomLoggerProvider();

            loggerFactory.AddProvider(customLogProvider);

            var tl = new TelemetryLogging(loggerFactory);

            var ex = new Exception("Test Exception level logging");

            tl.Debug("debug level logging message.");
            tl.Info("information level logging message.");
            tl.Warning("warning level logging message.");
            tl.Error("error level logging message.");
            tl.Exception(ex);

            Assert.True(customLogProvider.LogOutput.ContainsKey("NewRelic.Telemetry"));
            var logs = customLogProvider.LogOutput["NewRelic.Telemetry"];

            Assert.Equal(5, logs.Count);
            Assert.Contains("NewRelic Telemetry: debug level logging message.", logs);
            Assert.Contains("NewRelic Telemetry: information level logging message.", logs);
            Assert.Contains("NewRelic Telemetry: warning level logging message.", logs);
            Assert.Contains("NewRelic Telemetry: error level logging message.", logs);
            Assert.Contains($"NewRelic Telemetry: Exception {ex.GetType().FullName}: {ex.Message}", logs);
        }