Example #1
0
        public static Utf8ValueStringBuilder CreateUtf8StringBuilder()
        {
            var builder = new Utf8ValueStringBuilder();

            builder.Init(false);
            return(builder);
        }
        private async Task SendLogStringBuilder(Utf8ValueStringBuilder sb, CancellationToken cancellationToken)
        {
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, Endpoint)
            {
                Content = new GZipZStringContent(sb)
            };

            if (!string.IsNullOrEmpty(LicenseKey))
            {
                InternalLogger.Debug("Using license key specified with configuration");
                requestMessage.Headers.Add("X-License-Key", LicenseKey);
            }
            else if (!string.IsNullOrEmpty(InsertKey))
            {
                InternalLogger.Debug("Using insert key specified with configuration");
                requestMessage.Headers.Add("X-Insert-Key", InsertKey);
            }
            else
            {
                var lkey = Environment.GetEnvironmentVariable("NEW_RELIC_LICENSE_KEY");
                if (!string.IsNullOrEmpty(lkey))
                {
                    InternalLogger.Debug("Using licenseKey key specified with NEW_RELIC_LICENSE_KEY environment variable.");
                    requestMessage.Headers.Add("X-License-Key", lkey);
                }
                else
                {
                    var iKey = Environment.GetEnvironmentVariable("NEW_RELIC_INSERT_KEY");
                    if (!string.IsNullOrEmpty(iKey))
                    {
                        InternalLogger.Debug("Using insert key specified with NEW_RELIC_INSERT_KEY environment variable.");
                        requestMessage.Headers.Add("X-Insert-Key", iKey);
                    }
                    else
                    {
                        InternalLogger.Warn("No key found. Abort sending logs.");
                        return;
                    }
                }
            }

            if (InternalLogger.IsTraceEnabled)
            {
                InternalLogger.Trace("Sending content: {0}", sb.ToString());
            }
            var res = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);

            if (res.IsSuccessStatusCode)
            {
                if (InternalLogger.IsTraceEnabled)
                {
                    InternalLogger.Trace("HTTP POST succeeded: {0}", await res.Content.ReadAsStringAsync().ConfigureAwait(false));
                }
                return;
            }

            InternalLogger.Error("Send failed: {0} - {1}", res.StatusCode, await res.Content.ReadAsStringAsync().ConfigureAwait(false));
            throw new Exception($"HTTP POST failed with {res.StatusCode}.");
        }
 public GZipZStringContent(Utf8ValueStringBuilder sb)
 {
     Headers.TryAddWithoutValidation("Content-Type", "application/gzip");
     Headers.ContentEncoding.Add("gzip");
     this.sb = sb;
 }