/// <summary>
        /// Format the log events into a payload.
        /// </summary>
        /// <param name="logEvents">
        /// The events to format.
        /// </param>
        /// <param name="formatter">
        /// The formatter turning the log events into a textual representation.
        /// </param>
        /// <param name="output">
        /// The payload to send over the network.
        /// </param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Format(IEnumerable <LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
        {
            if (logEvents == null)
            {
                throw new ArgumentNullException(nameof(logEvents));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var events = logEvents as LogEvent[] ?? logEvents.ToArray();

            if (events.Length == 0)
            {
                return;
            }

            var batch = new LokiBatch();

            foreach (var logEvent in events)
            {
                var stream = batch.CreateStream();
                GenerateLabels(logEvent, stream);
                GenerateEntry(logEvent, stream);
            }

            if (batch.IsNotEmpty)
            {
                output.Write(batch.Serialize());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Format the log events into a payload.
        /// </summary>
        /// <param name="logEvents">
        /// The events to format.
        /// </param>
        /// <param name="formatter">
        /// The formatter turning the log events into a textual representation.
        /// </param>
        /// <param name="output">
        /// The payload to send over the network.
        /// </param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Format(IEnumerable <LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
        {
            if (logEvents == null)
            {
                throw new ArgumentNullException(nameof(logEvents));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var events = logEvents as LogEvent[] ?? logEvents.ToArray();

            if (events.Length == 0)
            {
                return;
            }

            var batch = new LokiBatch();

            // Group logEvent by labels
            var groups = events.Select(le => new { Labels = GenerateLabels(le), LogEvent = le })
                         .GroupBy(le => le.Labels, le => le.LogEvent, DictionaryComparer <string, string> .Instance);

            foreach (var group in groups)
            {
                var labels = group.Key;
                var stream = batch.CreateStream();
                foreach (var label in labels)
                {
                    stream.AddLabel(label.Key, label.Value);
                }

                foreach (var logEvent in group.OrderBy(x => x.Timestamp))
                {
                    GenerateEntry(logEvent, formatter, stream, labels.Keys);
                }
            }

            if (batch.IsNotEmpty)
            {
                output.Write(batch.Serialize());
            }
        }
        /// <summary>
        /// Format the log events into a payload.
        /// </summary>
        /// <param name="logEvents">
        /// The events to format.
        /// </param>
        /// <param name="output">
        /// The payload to send over the network.
        /// </param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Format(IEnumerable <string> logEvents, TextWriter output)
        {
            if (logEvents == null)
            {
                throw new ArgumentNullException(nameof(logEvents));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var events = logEvents as string[] ?? logEvents.ToArray();

            if (events.Length == 0)
            {
                return;
            }

            var batch = new LokiBatch();

            foreach (var logEvent in events)
            {
                var stream = batch.CreateStream();

                foreach (var label in _globalLabels)
                {
                    if (!IsExcluded(label.Key))
                    {
                        stream.AddLabel(label.Key, label.Value);
                    }
                }

                stream.AddEntry(DateTimeOffset.UtcNow, logEvent.TrimEnd('\r', '\n'));
            }

            if (batch.IsNotEmpty)
            {
                output.Write(batch.Serialize());
            }
        }
Exemplo n.º 4
0
    /// <summary>
    /// Format the log events into a payload.
    /// </summary>
    /// <param name="lokiLogEvents">
    /// The events to format wrapped in <see cref="LokiLogEvent"/>.
    /// </param>
    /// <param name="formatter">
    /// The formatter turning the log events into a textual representation.
    /// </param>
    /// <param name="output">
    /// The payload to send over the network.
    /// </param>
    /// <exception cref="ArgumentNullException">
    /// Thrown if one of params is null.
    /// </exception>
    public void Format(
        IReadOnlyCollection <LokiLogEvent> lokiLogEvents,
        ITextFormatter formatter,
        TextWriter output)
    {
        if (lokiLogEvents == null)
        {
            throw new ArgumentNullException(nameof(lokiLogEvents));
        }

        if (formatter == null)
        {
            throw new ArgumentNullException(nameof(formatter));
        }

        if (output == null)
        {
            throw new ArgumentNullException(nameof(output));
        }

        if (lokiLogEvents.Count == 0)
        {
            return;
        }

        var batch = new LokiBatch();

        // Group logEvent by labels
        var groups = lokiLogEvents
                     .Select(AddLevelAsPropertySafely)
                     .Select(GenerateLabels)
                     .GroupBy(
            le => le.Labels,
            le => le.LokiLogEvent,
            DictionaryComparer <string, string> .Instance);

        foreach (var group in groups)
        {
            var labels = group.Key;
            var stream = batch.CreateStream();

            foreach (var(key, value) in labels)
            {
                stream.AddLabel(key, value);
            }

            foreach (var logEvent in group.OrderBy(x => _useInternalTimestamp ? x.InternalTimestamp : x.LogEvent.Timestamp))
            {
                GenerateEntry(
                    logEvent,
                    formatter,
                    stream);
            }
        }

        if (batch.IsNotEmpty)
        {
            output.Write(batch.Serialize());
        }

        // Current behavior breaks rendering
        // Log.Info("Hero's {level}", 42)
        // Message: "Hero's \"info\""
        // level: "info"
        // _level: 42
        LokiLogEvent AddLevelAsPropertySafely(LokiLogEvent lokiLogEvent)
        {
            var logEvent = lokiLogEvent.LogEvent;

            logEvent.RenamePropertyIfPresent("level", _renamingStrategy.Rename);
            logEvent.AddOrUpdateProperty(
                new LogEventProperty("level", new ScalarValue(logEvent.Level.ToGrafanaLogLevel())));

            return(lokiLogEvent);
        }
    }