/// <summary>
        ///     Adds a Serilog sink that writes <see cref="Serilog.Events.LogEvent" /> to Apache Kafka using
        ///     a custom <see cref="ITextFormatter" />.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="formatter">A formatter to convert the log events into text for the kafka.</param>
        /// <param name="kafka">Options to configure communication with kafka.</param>
        /// <param name="batch">Options to configure sink write log events in batches.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Kafka(this LoggerSinkConfiguration sinkConfiguration,
                                                ITextFormatter formatter, KafkaOptions kafka, BatchOptions batch)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }

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

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

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

            var kafkaSink = KafkaSink.Create(formatter, kafka, batch);

            return(sinkConfiguration.Sink(kafkaSink));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a sink that writes log events to a Kafka topic in the broker endpoints.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param>
        /// <param name="period">The time in seconds to wait between checking for event batches.</param>
        /// <param name="bootstrapServers">The list of bootstrapServers separated by comma.</param>
        /// <param name="topic">The topic name.</param>
        /// <returns></returns>
        public static LoggerConfiguration Kafka(
            this LoggerSinkConfiguration loggerConfiguration,
            string bootstrapServers = "localhost:9092",
            int batchSizeLimit      = 50,
            int period = 5,
            SecurityProtocol securityProtocol = SecurityProtocol.Plaintext,
            SaslMechanism saslMechanism       = SaslMechanism.Plain,
            string topic         = "logs",
            string saslUsername  = null,
            string saslPassword  = null,
            string sslCaLocation = null)
        {
            var sink = new KafkaSink(
                bootstrapServers,
                batchSizeLimit,
                period,
                securityProtocol,
                saslMechanism,
                topic,
                saslUsername,
                saslPassword,
                sslCaLocation);

            return(loggerConfiguration.Sink(sink));
        }
        public static LoggerConfiguration Kafka(
            this LoggerSinkConfiguration loggerConfiguration,
            Func <LogEvent, string> topicDecider,
            string bootstrapServers = "localhost:9092",
            int batchSizeLimit      = 50,
            int period = 5,
            SecurityProtocol securityProtocol = SecurityProtocol.Plaintext,
            SaslMechanism saslMechanism       = SaslMechanism.Plain,
            string saslUsername                = null,
            string saslPassword                = null,
            string sslCaLocation               = null,
            ITextFormatter formatter           = null,
            LogEventLevel restrictedToMinLevel = LogEventLevel.Verbose,
            LoggingLevelSwitch levelSwitch     = null)
        {
            var sink = new KafkaSink(
                bootstrapServers,
                batchSizeLimit,
                period,
                securityProtocol,
                saslMechanism,
                topicDecider,
                saslUsername,
                saslPassword,
                sslCaLocation,
                formatter);

            return(loggerConfiguration.Sink(sink, restrictedToMinLevel, levelSwitch));
        }
        public static LoggerConfiguration Kafka(
            this LoggerSinkConfiguration loggerConfiguration,
            KafkaSinkOptions options)
        {
            Contract.Requires(loggerConfiguration != null);
            Contract.Requires(options != null);
            Contract.Ensures(Contract.Result<LoggerConfiguration>() != null);

            var kafkaClient = new KafkaClient(options);
            var kafkaSink = new KafkaSink(kafkaClient, options);
            var result = loggerConfiguration.Sink(kafkaSink);
            Contract.Assume(result != null);
            return result;
        }
        /// <summary>
        ///     Adds a Serilog sink that writes <see cref="Serilog.Events.LogEvent" /> to Apache Kafka using
        ///     a custom <see cref="ITextFormatter" />. In case of kafka unavailability,
        ///     <see cref="Serilog.Events.LogEvent" /> will be written to <paramref name="fallback" /> sink.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="formatter">A formatter to convert the log events into text for the kafka.</param>
        /// <param name="kafka">Options to configure communication with kafka.</param>
        /// <param name="batch">Options to configure sink write log events in batches.</param>
        /// <param name="fallbackTime">Time to wait between checking of kafka availability.</param>
        /// <param name="fallback">Fallback sink to write the log events when kafka is unavailable.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Kafka(this LoggerSinkConfiguration sinkConfiguration,
                                                ITextFormatter formatter, KafkaOptions kafka, BatchOptions batch, ILogEventSink fallback,
                                                TimeSpan fallbackTime)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }

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

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

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

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

            if (fallbackTime <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(fallbackTime), "The fallback time must be positive");
            }

            var kafkaSink = KafkaSink.Create(formatter, kafka, batch);

            return(sinkConfiguration.Sink(KafkaFailoverSink.Create(kafkaSink, fallback, batch, fallbackTime)));
        }
        private static LoggerConfiguration Kafka(
            this LoggerSinkConfiguration loggerConfiguration,
            string bootstrapServers,
            int batchSizeLimit,
            int period,
            SecurityProtocol securityProtocol,
            SaslMechanism saslMechanism,
            string saslUsername,
            string saslPassword,
            string sslCaLocation,
            string topic,
            Func <LogEvent, string> topicDecider,
            ITextFormatter formatter)
        {
            var kafkaSink = new KafkaSink(
                bootstrapServers,
                securityProtocol,
                saslMechanism,
                saslUsername,
                saslPassword,
                sslCaLocation,
                topic,
                topicDecider,
                formatter);

            var batchingOptions = new PeriodicBatchingSinkOptions
            {
                BatchSizeLimit = batchSizeLimit,
                Period         = TimeSpan.FromSeconds(period)
            };

            var batchingSink = new PeriodicBatchingSink(
                kafkaSink,
                batchingOptions);

            return(loggerConfiguration
                   .Sink(batchingSink));
        }