예제 #1
0
        internal static string FormatRawPayload(IEnumerable <LogEvent> events, long?eventBodyLimitBytes)
        {
            var payload = new StringWriter();

            payload.Write("{\"Events\":[");

            var delimStart = "";

            foreach (var logEvent in events)
            {
                var buffer = new StringWriter();

                try
                {
                    RawJsonFormatter.FormatContent(logEvent, buffer);
                }
                catch (Exception ex)
                {
                    LogNonFormattableEvent(logEvent, ex);
                    continue;
                }

                var json = buffer.ToString();
                if (CheckEventBodySize(json, eventBodyLimitBytes))
                {
                    payload.Write(delimStart);
                    payload.Write(json);
                    delimStart = ",";
                }
            }

            payload.Write("]}");
            return(payload.ToString());
        }
예제 #2
0
        internal static string FormatRawPayload(LogEvent logEvent)
        {
            var payload = new StringWriter();

            payload.Write("{\"Events\":[");
            RawJsonFormatter.FormatContent(logEvent, payload);
            payload.Write("]}");
            return(payload.ToString());
        }
예제 #3
0
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            _nextRequiredLevelCheckUtc = DateTime.UtcNow.Add(RequiredLevelCheckInterval);

            var payload = new StringWriter();

            payload.Write("{\"Events\":[");

            var delimStart = "";

            foreach (var logEvent in events)
            {
                if (_eventBodyLimitBytes.HasValue)
                {
                    var scratch = new StringWriter();
                    RawJsonFormatter.FormatContent(logEvent, scratch);
                    var buffered = scratch.ToString();

                    if (Encoding.UTF8.GetByteCount(buffered) > _eventBodyLimitBytes.Value)
                    {
                        SelfLog.WriteLine("Event JSON representation exceeds the byte size limit of {0} set for this sink and will be dropped; data: {1}", _eventBodyLimitBytes, buffered);
                    }
                    else
                    {
                        payload.Write(delimStart);
                        payload.Write(buffered);
                        delimStart = ",";
                    }
                }
                else
                {
                    payload.Write(delimStart);
                    RawJsonFormatter.FormatContent(logEvent, payload);
                    delimStart = ",";
                }
            }

            payload.Write("]}");

            var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");

            if (!string.IsNullOrWhiteSpace(_apiKey))
            {
                content.Headers.Add(ApiKeyHeaderName, _apiKey);
            }

            var result = await _httpClient.PostAsync(BulkUploadResource, content).ConfigureAwait(false);

            if (!result.IsSuccessStatusCode)
            {
                throw new LoggingFailedException($"Received failed result {result.StatusCode} when posting events to Seq");
            }

            var returned = await result.Content.ReadAsStringAsync();

            var minimumAcceptedLevel = SeqApi.ReadEventInputResult(returned);

            if (minimumAcceptedLevel == null)
            {
                if (_levelControlSwitch != null)
                {
                    _levelControlSwitch.MinimumLevel = LevelAlias.Minimum;
                }
            }
            else
            {
                if (_levelControlSwitch == null)
                {
                    _levelControlSwitch = new LoggingLevelSwitch(minimumAcceptedLevel.Value);
                }
                else
                {
                    _levelControlSwitch.MinimumLevel = minimumAcceptedLevel.Value;
                }
            }
        }