示例#1
0
        /// <summary>
        /// Emit a batch of log events, running asynchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
        {
            var payload = new StringWriter();
            payload.Write("{\"docs\":[");

            var formatter = new SimpleJsonFormatter(true);
            var delimStart = "{";
            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                var renderedMessage = logEvent.RenderedMessage;
                payload.Write(",\"UtcTimestamp\":\"{0:u}\",\"RenderedMessage\":\"{1}\"}}",
                    logEvent.Timestamp.ToUniversalTime().DateTime,
                    SimpleJsonFormatter.Escape(renderedMessage));
                delimStart = ",{";
            }

            payload.Write("]}");

            var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
            var result = await _httpClient.PostAsync(BulkUploadResource, content);
            if (!result.IsSuccessStatusCode)
                SelfLog.WriteLine("Received failed result {0}: {1}", result.StatusCode, result.Content.ReadAsStringAsync().Result);
        }
        static dynamic FormatJson(LogEvent @event)
        {
            var formatter = new SimpleJsonFormatter();
            var output = new StringWriter();
            formatter.Format(@event, output);

            var serializer = new JsonSerializer { DateParseHandling = DateParseHandling.None };
            return serializer.Deserialize(new JsonTextReader(new StringReader(output.ToString())));
        }
示例#3
0
        /// <summary>
        /// Emit a batch of log events, running to completion synchronously.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override void EmitBatch(IEnumerable<LogEvent> events)
        {
            var payload = new StringWriter();
            payload.Write("{\"d\":[");

            var formatter = new SimpleJsonFormatter(true);
            var delimStart = "{";
            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                var renderedMessage = logEvent.RenderedMessage;
                payload.Write(",\"UtcTimestamp\":\"{0:u}\",\"RenderedMessage\":\"{1}\"}}",
                              logEvent.Timestamp.ToUniversalTime().DateTime,
                              SimpleJsonFormatter.Escape(renderedMessage));
                delimStart = ",{";
            }

            payload.Write("]}");

            var bson = BsonDocument.Parse(payload.ToString());
            var docs = bson["d"].AsBsonArray;
            GetLogCollection().InsertBatch(docs);
        }
示例#4
0
        void OnTick()
        {
            var count = 0;
            var events = new Queue<LogEvent>();
            LogEvent next;
            while (count < MaxPost && _queue.TryDequeue(out next))
            {
                count++;
                events.Enqueue(next);
            }

            if (events.Count == 0)
                return;

            var payload = new StringWriter();
            payload.Write("{\"d\":[");

            var formatter = new SimpleJsonFormatter(true);
            var delimStart = "{";
            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                var renderedMessage = logEvent.RenderedMessage;
                payload.Write(",\"UtcTimeStamp\":\"{0:u}\",\"RenderedMessage\":\"{1}\"}}",
                    logEvent.TimeStamp.ToUniversalTime().DateTime,
                    renderedMessage);
                delimStart = ",{";
            }

            payload.Write("]}");

            try
            {
                var bson = BsonDocument.Parse(payload.ToString());
                var docs = bson["d"].AsBsonArray;
                LogCollection.InsertBatch(docs);
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch { }
            // ReSharper restore EmptyGeneralCatchClause
            finally
            {
                if (!_unloading)
                    SetTimer();
            }
        }
示例#5
0
        async Task OnTick()
        {
            var count = 0;
            var events = new Queue<LogEvent>();
            LogEvent next;
            while (count < MaxPost && _queue.TryDequeue(out next))
            {
                count++;
                events.Enqueue(next);
            }

            if (events.Count == 0)
                return;

            var payload = new StringWriter();
            payload.Write("{\"docs\":[");

            var formatter = new SimpleJsonFormatter(true);
            var delimStart = "{";
            foreach (var logEvent in events)
            {
                payload.Write(delimStart);
                formatter.Format(logEvent, payload);
                var renderedMessage = logEvent.RenderedMessage;
                payload.Write(",\"UtcTimeStamp\":\"{0:u}\",\"RenderedMessage\":\"{1}\"}}",
                    logEvent.TimeStamp.ToUniversalTime().DateTime,
                    renderedMessage);
                delimStart = ",{";
            }

            payload.Write("]}");

            try
            {
                var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
                var result = await _httpClient.PostAsync(BulkUploadResource, content);
                if (!result.IsSuccessStatusCode)
                    SelfLog.WriteLine("Received failed result {0}: {1}", result.StatusCode, result.Content.ReadAsStringAsync().Result);
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch { }
            // ReSharper restore EmptyGeneralCatchClause
            finally
            {
                if (!_unloading)
                    SetTimer();
            }
        }