示例#1
0
        private bool PostBatch(IEnumerable <TraceData> events, bool shouldThrow = true)
        {
            if (_associatedTraceListener.ServerUrl == null)
            {
                return(true);
            }

            var uri = _associatedTraceListener.ServerUrl;

            if (!uri.EndsWith("/"))
            {
                uri += "/";
            }
            uri += BulkUploadResource;

            //var request = (HttpWebRequest)WebRequest.Create(uri);
            var request = _httpWebRequestFactory.Create(uri);

            request.Method      = "POST";
            request.ContentType = "application/json; charset=utf-8";
            //if (!string.IsNullOrWhiteSpace(ApiKey))
            if (!string.IsNullOrEmpty(_associatedTraceListener.ApiKey))
            {
                //request.Headers.Add(ApiKeyHeaderName, ApiKey);
                request.AddHeader(ApiKeyHeaderName, _associatedTraceListener.ApiKey);
            }

            //var test = new StringWriter();
            //test.Write("{\"events\":[");
            //SeqPayloadFormatter.ToJson(events, test);
            //test.Write("]}");
            //var output = test.ToString();

            using (var requestStream = request.GetRequestStream())
                using (var payload = new StreamWriter(requestStream))
                {
                    payload.Write("{\"events\":[");
                    SeqPayloadFormatter.ToJson(events, payload);
                    payload.Write("]}");
                }

            //using (var response = (HttpWebResponse)request.GetResponse())
            using (var response = request.GetResponse())
            {
                var responseStream = response.GetResponseStream();
                if (responseStream == null)
                {
                    throw new WebException("No response was received from the Seq server");
                }

                using (var reader = new StreamReader(responseStream))
                {
                    var data = reader.ReadToEnd();
                    if ((int)response.StatusCode >= 300)
                    {
                        if (shouldThrow)
                        {
                            throw new WebException(string.Format("Received failed response {0} from Seq server: {1}",
                                                                 response.StatusCode,
                                                                 data));
                        }
                        return(false);
                    }
                    return(true);
                }
            }
        }
 private bool IsFormatterLiteral(object value)
 {
     return(SeqPayloadFormatter.IsLiteral(value));
 }