Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SumoLogicUnbufferedSink"/> class.
        /// </summary>
        public SumoLogicSinkTests()
        {
            _messagesHandler = new MockHttpMessageHandler();

            _sink = new SumoLogicSink(
                null,
                _messagesHandler,
                new SumoLogicConnection
            {
                Uri        = new Uri("http://www.fakeadress.com"),
                ClientName = "SumoLogicSinkTest",
            },
                new SumoLogicSource
            {
                SourceName     = "SumoLogicSinkTest",
                SourceCategory = "SumoLogicSinkSourceCategory",
                SourceHost     = "SumoLogicSinkSourceHost",
            },
                new MessageTemplateTextFormatter("{Level:u}: {Message}", CultureInfo.InvariantCulture));

            _logger = new LoggerConfiguration()
                      .MinimumLevel.Debug()
                      .WriteTo.Sink(_sink)
                      .CreateLogger();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Setups the logger with the <see cref="SumoLogicSink"/> based on the given settings.
        /// </summary>
        /// <param name="messagesPerRequest">The maximum messages per request.</param>
        /// <param name="maxFlushInterval">The maximum flush interval, in milliseconds.</param>
        /// <param name="flushingAccuracy">The flushing accuracy, in milliseconds.</param>
        /// <param name="retryInterval">The retry interval, in milliseconds.</param>
        private void SetUpLogger(long messagesPerRequest, long maxFlushInterval, long flushingAccuracy, long retryInterval = 10000)
        {
            _messagesHandler = new MockHttpMessageHandler();

            _sink = new SumoLogicSink(
                null,
                _messagesHandler,
                new SumoLogicConnection
            {
                Uri                = new Uri("http://www.fakeadress.com"),
                ClientName         = "BufferedSumoLogicSinkTest",
                MessagesPerRequest = messagesPerRequest,
                MaxFlushInterval   = TimeSpan.FromMilliseconds(maxFlushInterval),
                FlushingAccuracy   = TimeSpan.FromMilliseconds(flushingAccuracy),
                RetryInterval      = TimeSpan.FromMilliseconds(retryInterval),
            },
                new SumoLogicSource
            {
                SourceName     = "BufferedSumoLogicSinkTest",
                SourceCategory = "BufferedSumoLogicSinkSourceCategory",
                SourceHost     = "BufferedSumoLogicSinkSourceHost",
            },
                new MessageTemplateTextFormatter("{Level:u}: {Message}", CultureInfo.InvariantCulture));

            _logger = new LoggerConfiguration()
                      .MinimumLevel.Information()
                      .WriteTo.Sink(_sink)
                      .CreateLogger();
        }
        /// <summary>
        /// Adds the WriteTo.SumoLogic() extension method to <see cref="LoggerConfiguration"/>.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration</param>
        /// <param name="endpointUrl">Sumo Logic endpoint URL to send logs to</param>
        /// <param name="sourceName">Sumo Logic source name</param>
        /// <param name="sourceCategory">Sumo Logic source category</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <returns></returns>
        public static LoggerConfiguration SumoLogic(
            this LoggerSinkConfiguration loggerConfiguration,
            string endpointUrl,
            string sourceName     = SumoLogicSink.DefaultSourceName,
            string sourceCategory = SumoLogicSink.DefaultSourceCategory,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchSizeLimit             = SumoLogicSink.DefaultBatchSizeLimit,
            TimeSpan?period                = null,
            string outputTemplate          = SumoLogicSink.DefaultOutputTemplate,
            IFormatProvider formatProvider = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }

            if (string.IsNullOrEmpty(endpointUrl))
            {
                throw new ArgumentNullException(nameof(endpointUrl));
            }

            var defaultPeriod = period ?? SumoLogicSink.DefaultPeriod;
            var formatter     = new MessageTemplateTextFormatter(outputTemplate, formatProvider);

            var sink = new SumoLogicSink(
                endpointUrl,
                sourceName,
                sourceCategory,
                formatter,
                batchSizeLimit,
                defaultPeriod);

            return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
        }
        /// <summary>
        /// Adds the WriteTo.SumoLogic() extension method to <see cref="LoggerConfiguration"/>.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration</param>
        /// <param name="endpointUrl">Sumo Logic endpoint URL to send logs to</param>
        /// <param name="sourceName">Sumo Logic source name</param>
        /// <param name="sourceCategory">Sumo Logic source category</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="textFormatter">Supplies how logs should be formatted, or null to use the default</param>
        /// <param name="outputTemplate">Override default output template. Should not be used if overriding <see cref="ITextFormatter"/></param>
        /// <param name="handler">Override default http handler <see cref="HttpMessageHandler"/></param>
        /// <returns></returns>
        public static LoggerConfiguration SumoLogic(
            this LoggerSinkConfiguration loggerConfiguration,
            string endpointUrl,
            string sourceName     = SumoLogicSink.DefaultSourceName,
            string sourceCategory = SumoLogicSink.DefaultSourceCategory,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchSizeLimit           = SumoLogicSink.DefaultBatchSizeLimit,
            TimeSpan?period              = null,
            ITextFormatter textFormatter = null,
            string outputTemplate        = null,
            HttpMessageHandler handler   = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }

            if (string.IsNullOrWhiteSpace(endpointUrl))
            {
                throw new ArgumentNullException(nameof(endpointUrl));
            }

            period = period ?? SumoLogicSink.DefaultPeriod;

            /* Order:
             * 1. Use textFormatter if set
             * 2. Use outputTemplate if set and textFormatter not set
             * 3. If neither set, use default message formatter
             */
            if (textFormatter == null)
            {
                var templateToUse = !string.IsNullOrWhiteSpace(outputTemplate)
                    ? outputTemplate
                    : SumoLogicSink.DefaultOutputTemplate;

                textFormatter = new MessageTemplateTextFormatter(templateToUse, null);
            }

            var sink = new SumoLogicSink(
                endpointUrl,
                sourceName,
                sourceCategory,
                textFormatter,
                batchSizeLimit,
                period.Value,
                handler);

            return(loggerConfiguration.Sink(sink, restrictedToMinimumLevel));
        }