/// <param name="uri">Splunk server uri, for example https://localhost:8088.</param>
        /// <param name="token">HTTP event collector authorization token.</param>
        /// <param name="metadata">Logger metadata.</param>
        /// <param name="sendMode">Send mode of the events.</param>
        /// <param name="batchInterval">Batch interval in milliseconds.</param>
        /// <param name="batchSizeBytes">Batch max size.</param>
        /// <param name="batchSizeCount">Max number of individual events in batch.</param>
        /// <param name="ignoreSslErrors">Server validation callback should always return true</param>
        /// <param name="middleware">
        /// HTTP client middleware. This allows to plug an HttpClient handler that
        /// intercepts logging HTTP traffic.
        /// </param>
        /// <param name="formatter"></param>
        /// <remarks>
        /// Zero values for the batching params mean that batching is off.
        /// </remarks>
        public HttpEventCollectorSender(
            Uri uri, string token, HttpEventCollectorEventInfo.Metadata metadata,
            SendMode sendMode,
            int batchInterval, int batchSizeBytes, int batchSizeCount, bool ignoreSslErrors,
            HttpEventCollectorMiddleware middleware,
            HttpEventCollectorFormatter formatter = null)
        {
            this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
            this.jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            this.jsonSerializerSettings.Formatting            = Formatting.None;
            this.jsonSerializerSettings.Converters            = new[] { new Newtonsoft.Json.Converters.StringEnumConverter() };
            this.jsonSerializer = JsonSerializer.CreateDefault(this.jsonSerializerSettings);
            this.sendMode       = sendMode;
            this.batchInterval  = batchInterval;
            this.batchSizeBytes = batchSizeBytes;
            this.batchSizeCount = batchSizeCount;
            this.metadata       = metadata;
            this.token          = token;
            this.middleware     = middleware;
            this.formatter      = formatter;

            // special case - if batch interval is specified without size and count
            // they are set to "infinity", i.e., batch may have any size
            if (this.batchInterval > 0 && this.batchSizeBytes == 0 && this.batchSizeCount == 0)
            {
                this.batchSizeBytes = this.batchSizeCount = int.MaxValue;
            }

            // when size configuration setting is missing it's treated as "infinity",
            // i.e., any value is accepted.
            if (this.batchSizeCount == 0 && this.batchSizeBytes > 0)
            {
                this.batchSizeCount = int.MaxValue;
            }
            else if (this.batchSizeBytes == 0 && this.batchSizeCount > 0)
            {
                this.batchSizeBytes = int.MaxValue;
            }

            // setup the timer
            if (batchInterval != 0) // 0 means - no timer
            {
                timer = new Timer(OnTimer, null, batchInterval, batchInterval);
            }

            // setup HTTP client
            try
            {
                var httpMessageHandler = ignoreSslErrors ? BuildHttpMessageHandler(ignoreSslErrors) : null;
                httpClient = httpMessageHandler != null ? new HttpClient(httpMessageHandler) : new HttpClient();
            }
            catch
            {
                // Fallback on PlatformNotSupported and other funny exceptions
                httpClient = new HttpClient();
            }

            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue(AuthorizationHeaderScheme, token);
        }
        public HttpEventCollectorSender(
            Uri uri,
            string token,
            HttpEventCollectorEventInfo.Metadata metadata,
            SendMode sendMode,
            int batchInterval,
            int batchSizeBytes,
            int batchSizeCount,
            HttpEventCollectorMiddleware middleware,
            HttpEventCollectorFormatter formatter = null,
            bool ignoreCertificateErrors          = false
            )
        {
            this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
            this.sendMode       = sendMode;
            this.batchSizeBytes = batchSizeBytes;
            this.batchSizeCount = batchSizeCount;
            this.metadata       = metadata;
            this.token          = token;
            this.middleware     = middleware;
            this.formatter      = formatter;

            if (batchInterval > 0 && this.batchSizeBytes == 0 && this.batchSizeCount == 0)
            {
                this.batchSizeBytes = this.batchSizeCount = int.MaxValue;
            }
            if (this.batchSizeCount == 0 && this.batchSizeBytes > 0)
            {
                this.batchSizeCount = int.MaxValue;
            }
            else if (this.batchSizeBytes == 0 && this.batchSizeCount > 0)
            {
                this.batchSizeBytes = int.MaxValue;
            }
            if (batchInterval != 0)
            {
                timer = new Timer(OnTimer, null, batchInterval, batchInterval);
            }

            if (ignoreCertificateErrors)
            {
                var certificateHandler = new HttpClientHandler
                {
                    ClientCertificateOptions = ClientCertificateOption.Manual,
                    ServerCertificateCustomValidationCallback =
                        (httpRequestMessage, cert, cetChain, policyErrors) => true
                };
                httpClient = new HttpClient(certificateHandler, true);
            }
            else
            {
                httpClient = new HttpClient();
            }

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                AuthorizationHeaderScheme,
                token
                );
        }
Exemplo n.º 3
0
        /// <param name="uri">Splunk server uri, for example https://localhost:8088.</param>
        /// <param name="token">HTTP event collector authorization token.</param>
        /// <param name="metadata">Logger metadata.</param>
        /// <param name="sendMode">Send mode of the events.</param>
        /// <param name="batchInterval">Batch interval in milliseconds.</param>
        /// <param name="batchSizeBytes">Batch max size.</param>
        /// <param name="batchSizeCount">Max number of individual events in batch.</param>
        /// <param name="middleware">
        /// HTTP client middleware. This allows to plug an HttpClient handler that
        /// intercepts logging HTTP traffic.
        /// </param>
        /// <remarks>
        /// Zero values for the batching params mean that batching is off.
        /// </remarks>
        public HttpEventCollectorSender(
            Uri uri, string token, HttpEventCollectorEventInfo.Metadata metadata,
            SendMode sendMode,
            int batchInterval, int batchSizeBytes, int batchSizeCount,
            HttpEventCollectorMiddleware middleware,
            HttpEventCollectorFormatter formatter = null)
        {
            this.serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.ContractResolver  = new CamelCasePropertyNamesContractResolver();

            this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
            this.sendMode       = sendMode;
            this.batchInterval  = batchInterval;
            this.batchSizeBytes = batchSizeBytes;
            this.batchSizeCount = batchSizeCount;
            this.metadata       = metadata;
            this.token          = token;
            this.middleware     = middleware;
            this.formatter      = formatter;

            // special case - if batch interval is specified without size and count
            // they are set to "infinity", i.e., batch may have any size
            if (this.batchInterval > 0 && this.batchSizeBytes == 0 && this.batchSizeCount == 0)
            {
                this.batchSizeBytes = this.batchSizeCount = int.MaxValue;
            }

            // when size configuration setting is missing it's treated as "infinity",
            // i.e., any value is accepted.
            if (this.batchSizeCount == 0 && this.batchSizeBytes > 0)
            {
                this.batchSizeCount = int.MaxValue;
            }
            else if (this.batchSizeBytes == 0 && this.batchSizeCount > 0)
            {
                this.batchSizeBytes = int.MaxValue;
            }

            // setup the timer
            if (batchInterval != 0) // 0 means - no timer
            {
                timer = new Timer(OnTimer, null, batchInterval, batchInterval);
            }

            // setup HTTP client
            httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue(AuthorizationHeaderScheme, token);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpEventCollectorSender"/> class.
        /// </summary>
        /// <param name="uri">Splunk server uri, for example https://localhost:8088.</param>
        /// <param name="token">HTTP event collector authorization token.</param>
        /// <param name="channel">HTTP event collector data channel.</param>
        /// <param name="metadata">Logger metadata.</param>
        /// <param name="sendMode">Send mode of the events.</param>
        /// <param name="batchInterval">Batch interval in milliseconds.</param>
        /// <param name="batchSizeBytes">Batch max size.</param>
        /// <param name="batchSizeCount">Max number of individual events in batch.</param>
        /// <param name="ignoreSslErrors">Server validation callback should always return true</param>
        /// <param name="useProxy">Default web proxy is used if set to true; otherwise, no proxy is used</param>
        /// <param name="middleware">HTTP client middleware. This allows to plug an HttpClient handler that
        /// intercepts logging HTTP traffic.</param>
        /// <param name="formatter">The formatter.</param>
        /// <remarks>
        /// Zero values for the batching params mean that batching is off.
        /// </remarks>
        public HttpEventCollectorSender(
            Uri uri,
            string token,
            string channel,
            HttpEventCollectorEventInfo.Metadata metadata,
            SendMode sendMode,
            int batchInterval,
            int batchSizeBytes,
            int batchSizeCount,
            bool ignoreSslErrors,
            bool useProxy,
            int maxConnectionsPerServer,
            HttpEventCollectorMiddleware middleware,
            HttpEventCollectorFormatter formatter = null,
            bool httpVersion10Hack = false)
        {
            NLog.Common.InternalLogger.Debug("Initializing Splunk HttpEventCollectorSender");

            this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
            this.jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            this.jsonSerializerSettings.Formatting            = Formatting.None;
            this.jsonSerializerSettings.Converters            = new[] { new Newtonsoft.Json.Converters.StringEnumConverter() };
            this.jsonSerializer         = JsonSerializer.CreateDefault(this.jsonSerializerSettings);
            this.sendMode               = sendMode;
            this.batchInterval          = batchInterval;
            this.batchSizeBytes         = batchSizeBytes;
            this.batchSizeCount         = batchSizeCount;
            this.metadata               = metadata;
            this.token                  = token;
            this.channel                = channel;
            this.middleware             = middleware;
            this.formatter              = formatter;
            this.applyHttpVersion10Hack = httpVersion10Hack;

            // special case - if batch interval is specified without size and count
            // they are set to "infinity", i.e., batch may have any size
            if (this.batchInterval > 0 && this.batchSizeBytes == 0 && this.batchSizeCount == 0)
            {
                this.batchSizeBytes = this.batchSizeCount = int.MaxValue;
            }

            // when size configuration setting is missing it's treated as "infinity",
            // i.e., any value is accepted.
            if (this.batchSizeCount == 0 && this.batchSizeBytes > 0)
            {
                this.batchSizeCount = int.MaxValue;
            }
            else if (this.batchSizeBytes == 0 && this.batchSizeCount > 0)
            {
                this.batchSizeBytes = int.MaxValue;
            }

            // setup the timer
            if (batchInterval != 0) // 0 means - no timer
            {
                timer = new Timer(OnTimer, null, batchInterval, batchInterval);
            }

            // setup HTTP client
            try
            {
                var httpMessageHandler = BuildHttpMessageHandler(ignoreSslErrors, useProxy, maxConnectionsPerServer);
                httpClient = new HttpClient(httpMessageHandler);
            }
            catch
            {
                // Fallback on PlatformNotSupported and other funny exceptions
                httpClient = new HttpClient();
            }

            // Enable TLS 1.2
            NLog.Common.InternalLogger.Debug("Setting TLS1.2 SecurityProtocolType");
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

            // setup splunk header token
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthorizationHeaderScheme, token);

            if (this.applyHttpVersion10Hack)
            {
                httpClient.BaseAddress = uri;
                httpClient.DefaultRequestHeaders.ConnectionClose = false;
                httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
            }

            // setup splunk channel request header
            if (!string.IsNullOrWhiteSpace(channel))
            {
                httpClient.DefaultRequestHeaders.Add(ChannelRequestHeaderName, channel);
            }
        }
        /// <param name="uri">Splunk server uri, for example https://localhost:8089.</param>
        /// <param name="token">HTTP event collector authorization token.</param>
        /// <param name="metadata">Logger metadata.</param>
        /// <param name="sendMode">Send mode of the events.</param>
        /// <param name="batchInterval">Batch interval in milliseconds.</param>
        /// <param name="batchSizeBytes">Batch max size.</param>
        /// <param name="batchSizeCount">MNax number of individual events in batch.</param>
        /// <param name="middleware">
        /// HTTP client middleware. This allows to plug an HttpClient handler that 
        /// intercepts logging HTTP traffic.
        /// </param>
        /// <remarks>
        /// Zero values for the batching params mean that batching is off. 
        /// </remarks>
        public HttpEventCollectorSender(
            Uri uri, string token, HttpEventCollectorEventInfo.Metadata metadata,
            SendMode sendMode,
            int batchInterval, int batchSizeBytes, int batchSizeCount,
            HttpEventCollectorMiddleware middleware)
        {
            this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
            this.sendMode = sendMode;
            this.batchInterval = batchInterval;
            this.batchSizeBytes = batchSizeBytes;
            this.batchSizeCount = batchSizeCount;
            this.metadata = metadata;
            this.token = token;
            this.middleware = middleware;

            // special case - if batch interval is specified without size and count
            // they are set to "infinity", i.e., batch may have any size 
            if (this.batchInterval > 0 && this.batchSizeBytes == 0 && this.batchSizeCount == 0)
            {
                this.batchSizeBytes = this.batchSizeCount = int.MaxValue;
            }

            // when size configuration setting is missing it's treated as "infinity",
            // i.e., any value is accepted.
            if (this.batchSizeCount == 0 && this.batchSizeBytes > 0)
            {
                this.batchSizeCount = int.MaxValue;
            }
            else if (this.batchSizeBytes == 0 && this.batchSizeCount > 0)
            {
                this.batchSizeBytes = int.MaxValue;
            }

            // setup the timer
            if (batchInterval != 0) // 0 means - no timer
            {
                timer = new Timer(OnTimer, null, batchInterval, batchInterval);
            }

            // setup HTTP client
            httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue(AuthorizationHeaderScheme, token);
        }