Example #1
0
        private static string Serialize(LogEvent logEvent)
        {
            var writer    = new StringWriter();
            var formatter = new NormalRenderedTextFormatter();

            formatter.Format(logEvent, writer);
            return(writer.ToString());
        }
        /// <summary>
        /// Adds a non-durable sink that sends log events using HTTP POST over the network. A
        /// non-durable sink will lose data after a system or process restart.
        /// </summary>
        /// <param name="sinkConfiguration">The logger configuration.</param>
        /// <param name="requestUri">The URI the request is sent to.</param>
        /// <param name="batchPostingLimit">
        /// The maximum number of events to post in a single batch. Default value is 1000.
        /// </param>
        /// <param name="queueLimit">
        /// The maximum number of events stored in the queue in memory, waiting to be posted over
        /// the network. Default value is infinitely.
        /// </param>
        /// <param name="period">
        /// The time to wait between checking for event batches. Default value is 2 seconds.
        /// </param>
        /// <param name="textFormatter">
        /// The formatter rendering individual log events into text, for example JSON. Default
        /// value is <see cref="NormalRenderedTextFormatter"/>.
        /// </param>
        /// <param name="batchFormatter">
        /// The formatter batching multiple log events into a payload that can be sent over the
        /// network. Default value is <see cref="DefaultBatchFormatter"/>.
        /// </param>
        /// <param name="restrictedToMinimumLevel">
        /// The minimum level for events passed through the sink. Default value is
        /// <see cref="LevelAlias.Minimum"/>.
        /// </param>
        /// <param name="httpClient">
        /// A custom <see cref="IHttpClient"/> implementation. Default value is
        /// <see cref="HttpClient"/>.
        /// </param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>>
        public static LoggerConfiguration Site24x7Url(
            this LoggerSinkConfiguration sinkConfiguration,
            string requestUri,
            string minimumLogLevel = null,
            TimeSpan?period        = null,
            int batchPostingLimit  = 1,
            int?queueLimit         = null
            )

        {
            LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose;

            if (minimumLogLevel == null)
            {
                restrictedToMinimumLevel = LogEventLevel.Verbose;
            }
            else if (minimumLogLevel == "Debug")
            {
                restrictedToMinimumLevel = LogEventLevel.Debug;
            }
            else if (minimumLogLevel == "Information")
            {
                restrictedToMinimumLevel = LogEventLevel.Information;
            }
            else if (minimumLogLevel == "Warning")
            {
                restrictedToMinimumLevel = LogEventLevel.Warning;
            }
            else if (minimumLogLevel == "Error")
            {
                restrictedToMinimumLevel = LogEventLevel.Error;
            }
            else if (minimumLogLevel == "Fatal")
            {
                restrictedToMinimumLevel = LogEventLevel.Fatal;
            }
            else
            {
                restrictedToMinimumLevel = LogEventLevel.Verbose;
            }

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

            // Default values
            period = period ?? TimeSpan.FromMilliseconds(0.01);
            ITextFormatter  textFormatter  = new NormalRenderedTextFormatter();
            IBatchFormatter batchFormatter = new DefaultBatchFormatter();
            IHttpClient     httpClient     = new DefaultHttpClient();

            var sink = queueLimit != null
                ? new HttpSink(requestUri, batchPostingLimit, queueLimit.Value, period.Value, textFormatter, batchFormatter, httpClient)
                : new HttpSink(requestUri, batchPostingLimit, period.Value, textFormatter, batchFormatter, httpClient);

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