コード例 #1
0
        public async Task <bool> WriteAsync(IReadOnlyCollection <EventData> events, CancellationToken cancellationToken)
        {
            Guard.ArgumentNotNull(events, "events");

            if (!events.Any())
            {
                return(false);
            }

            try
            {
                string logMessages;
                using (var serializer = new ElasticSearchEventDataSerializer(_index, _type))
                {
                    logMessages = serializer.Serialize(events);
                }

                using (var client = new HttpClient())
                {
                    var resp = await _retryPolicy.ExecuteAsync(async() =>
                    {
                        var content = new StringContent(logMessages);
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        var response = await client.PostAsync(_elasticsearchUrl, content, cancellationToken).ConfigureAwait(false);

                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            WarmStorageEventSource.Log.WriteToElasticSearchFailedPerf(events.Count);

                            await HandleErrorResponse(response);
                        }

                        //This is for retry strategy. If response is not successful it will raise an exception catched by the retry strategy.
                        response.EnsureSuccessStatusCodeEx();

                        WarmStorageEventSource.Log.WriteToElasticSearchSuccessPerf(events.Count);

                        return(response);
                    });
                }

                return(true);
            }
            catch (Exception ex)
            {
                //If a non-transient error has ocurred or if we ran out of attempts.

                WarmStorageEventSource.Log.WriteToElasticSearchError(ex);

                return(false);
            }
        }
コード例 #2
0
        public async Task<bool> WriteAsync(IReadOnlyCollection<EventData> events, CancellationToken cancellationToken)
        {
            Guard.ArgumentNotNull(events, "events");

            if(!events.Any())
            {
                return false;
            }

            try
            {
                string logMessages;
                using (var serializer = new ElasticSearchEventDataSerializer(_index, _type))
                {
                    logMessages = serializer.Serialize(events);
                }

                using (var client = new HttpClient())
                {
                    var resp = await _retryPolicy.ExecuteAsync(async () =>
                    {
                        var content = new StringContent(logMessages);
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        var response = await client.PostAsync(_elasticsearchUrl, content, cancellationToken).ConfigureAwait(false);

                        if(response.StatusCode != HttpStatusCode.OK)
                        {
                            WarmStorageEventSource.Log.WriteToElasticSearchFailedPerf(events.Count);

                            await HandleErrorResponse(response);
                        }

                        //This is for retry strategy. If response is not successful it will raise an exception catched by the retry strategy.
                        response.EnsureSuccessStatusCodeEx();

                        WarmStorageEventSource.Log.WriteToElasticSearchSuccessPerf(events.Count);

                        return response;
                    });
                }

                return true;
            }
            catch (Exception ex)
            {
                //If a non-transient error has ocurred or if we ran out of attempts.

                WarmStorageEventSource.Log.WriteToElasticSearchError(ex);

                return false;
            }
        }