public async Task SendMessageAsync(GelfMessage message)
        {
            var content = new StringContent(message.ToJson(), Encoding.UTF8, "application/json");
            var result  = await _httpClient.PostAsync("gelf", content).ConfigureAwait(false);

            result.EnsureSuccessStatusCode();
        }
Esempio n. 2
0
 public void SendMessage(GelfMessage message)
 {
     if (!_messageBuffer.Post(message))
     {
         Debug.Fail("Failed to add GELF message to buffer.");
     }
 }
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state,
                                 Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            var additionalFields = _options.AdditionalFields
                                   .Concat(GetScopeAdditionalFields())
                                   .Concat(GetStateAdditionalFields(state));

            var message = new GelfMessage
            {
                ShortMessage     = formatter(state, exception),
                Host             = _options.LogSource,
                Level            = GetLevel(logLevel),
                Timestamp        = GetTimestamp(),
                Logger           = _name,
                Exception        = exception?.ToString(),
                AdditionalFields = ValidateAdditionalFields(additionalFields).ToArray()
            };

            if (eventId != default)
            {
                message.EventId   = eventId.Id;
                message.EventName = eventId.Name;
            }

            _messageProcessor.SendMessage(message);
        }
Esempio n. 4
0
        public static string ToJson(this GelfMessage message)
        {
            using var stream     = new MemoryStream();
            using var jsonWriter = new Utf8JsonWriter(stream);

            jsonWriter.WriteStartObject();
            jsonWriter.WriteStringUnlessNull("version", message.Version);
            jsonWriter.WriteStringUnlessNull("host", message.Host);
            jsonWriter.WriteStringUnlessNull("short_message", message.ShortMessage);
            jsonWriter.WriteNumber("timestamp", message.Timestamp);
            jsonWriter.WriteNumber("level", (int)message.Level);
            jsonWriter.WriteStringUnlessNull("_logger", message.Logger);
            jsonWriter.WriteStringUnlessNull("_exception", message.Exception);
            jsonWriter.WriteNumberUnlessNull("_event_id", message.EventId);
            jsonWriter.WriteStringUnlessNull("_event_name", message.EventName);

            foreach (var field in message.AdditionalFields)
            {
                WriteAdditionalField(jsonWriter, field);
            }

            jsonWriter.WriteEndObject();
            jsonWriter.Flush();

            return(Encoding.UTF8.GetString(stream.ToArray()));
        }
Esempio n. 5
0
        private static byte[] GetMessageBytes(GelfMessage message)
        {
            var messageJson = JObject.FromObject(message);

            foreach (var field in message.AdditionalFields)
            {
                messageJson[$"_{field.Key}"] = field.Value?.ToString();
            }

            return(Encoding.UTF8.GetBytes(messageJson.ToString()));
        }
Esempio n. 6
0
        public async Task SendMessageAsync(GelfMessage message)
        {
            var messageBytes = Encoding.UTF8.GetBytes(message.ToJson());

            if (_options.CompressUdp && messageBytes.Length > _options.UdpCompressionThreshold)
            {
                messageBytes = await CompressMessageAsync(messageBytes);
            }

            foreach (var messageChunk in ChunkMessage(messageBytes))
            {
                await _udpClient.SendAsync(messageChunk, messageChunk.Length, _options.Host, _options.Port);
            }
        }
Esempio n. 7
0
        public async Task SendMessageAsync(GelfMessage message)
        {
            var messageBytes = GetMessageBytes(message);

            if (_options.Compress && messageBytes.Length > _options.CompressionThreshold)
            {
                messageBytes = await CompressMessageAsync(messageBytes).ConfigureAwait(false);
            }

            foreach (var messageChunk in ChunkMessage(messageBytes))
            {
                await _udpClient.SendAsync(messageChunk, messageChunk.Length, _options.Host, _options.Port)
                .ConfigureAwait(false);
            }
        }
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state,
                                 Exception exception, Func <TState, Exception, string> formatter)
        {
            if (logLevel == LogLevel.None || !IsEnabled(logLevel))
            {
                return;
            }

            var message = new GelfMessage
            {
                ShortMessage     = formatter(state, exception),
                Host             = _options.LogSource,
                Level            = GetLevel(logLevel),
                Timestamp        = GetTimestamp(),
                AdditionalFields = _options.AdditionalFields
                                   .Concat(GetStateAdditionalFields(state, exception))
                                   .Concat(GetScopeAdditionalFields())
            };

            _messageProcessor.SendMessage(message);
        }
        public static string ToJson(this GelfMessage message)
        {
            var messageJson = JObject.FromObject(message);

            foreach (var field in message.AdditionalFields)
            {
                if (IsNumeric(field.Value))
                {
                    messageJson[$"_{field.Key}"] = JToken.FromObject(field.Value);
                }
                else
                {
                    messageJson[$"_{field.Key}"] = field.Value?.ToString();
                }
            }

            return(JsonConvert.SerializeObject(messageJson, Formatting.None, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }));
        }