/// <summary>
        /// Gets the meta data.
        /// </summary>
        /// <returns></returns>
        private HttpEventCollectorEventInfo.Metadata GetMetaData(string index, string source, string sourcetype)
        {
            var hostName = _hostName ?? (_hostName = GetMachineName());
            if (!_metaData.TryGetValue(source ?? string.Empty, out var metaData))
            {
                if (_metaData.Count > 1000)
                    _metaData.Clear();  // Extreme case that should never happen
                metaData = new HttpEventCollectorEventInfo.Metadata(string.IsNullOrEmpty(index) ? null : index, string.IsNullOrEmpty(source) ? null : source, sourcetype, hostName);
                _metaData[source ?? string.Empty] = metaData;
            }

            return metaData;
        }
        /// <summary>
        /// Gets the meta data.
        /// </summary>
        /// <param name="loggerName">Name of the logger.</param>
        /// <returns></returns>
        private HttpEventCollectorEventInfo.Metadata GetMetaData(string loggerName)
        {
            var hostName = _hostName ?? (_hostName = GetMachineName());

            if (!_metaData.TryGetValue(loggerName ?? string.Empty, out var metaData))
            {
                if (_metaData.Count > 1000)
                {
                    _metaData.Clear();  // Extreme case that should never happen
                }
                metaData = new HttpEventCollectorEventInfo.Metadata(null, string.IsNullOrEmpty(loggerName) ? null : loggerName, "_json", hostName);
                _metaData[loggerName ?? string.Empty] = metaData;
            }

            return(metaData);
        }
        protected override void Write(LogEventInfo logEventInfo)
        {
            // Sanity check for LogEventInfo
            if (logEventInfo == null)
            {
                throw new ArgumentNullException(nameof(logEventInfo));
            }

            // Make sure we have a properly setup HttpEventCollectorSender
            if (_hecSender == null)
            {
                throw new NLogRuntimeException("SplunkHttpEventCollector SendEventToServer() called before InitializeTarget()");
            }

            // Build metaData
            var metaData = new HttpEventCollectorEventInfo.Metadata(Index, (Source ?? logEventInfo.LoggerName), (SourceType ?? logEventInfo.LoggerName), GetMachineName());

            // Build properties object and add standard values
            var properties = new Dictionary <String, object>
            {
                { "Source", (Source ?? logEventInfo.LoggerName) },
                { "SourceType", (SourceType ?? logEventInfo.LoggerName) },
                { "Host", GetMachineName() }
            };

            // add attached properties
            if (logEventInfo.HasProperties)
            {
                foreach (var key in logEventInfo.Properties.Keys)
                {
                    properties.Add(key.ToString(), logEventInfo.Properties[key]);
                }
            }

            // add parameters
            if (logEventInfo.Parameters != null && logEventInfo.Parameters.Length > 0)
            {
                for (int i = 0; i < logEventInfo.Parameters.Length; i++)
                {
                    properties.Add("{" + i + "}", logEventInfo.Parameters[i]);
                }
            }

            // Send the event to splunk
            _hecSender.Send(null, logEventInfo.Level.Name, logEventInfo.Message, logEventInfo.FormattedMessage, logEventInfo.Exception, properties, metaData);
            _hecSender.FlushSync();
        }
        protected override void Append(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
            {
                throw new ArgumentNullException(nameof(loggingEvent));
            }

            if (_hecSender == null)
            {
                throw new Exception("SplunkHttpEventCollector Append() called before ActivateOptions()");
            }

            var metaData = new HttpEventCollectorEventInfo.Metadata(
                Index,
                loggingEvent.LoggerName,
                "_json",
                GetMachineName()
                );

            var properties = new Dictionary <String, object>
            {
                { "Source", loggingEvent.LoggerName }, { "Host", GetMachineName() }
            };

            if (loggingEvent.Properties != null && loggingEvent.Properties.Count > 0)
            {
                foreach (var key in loggingEvent.Properties.GetKeys())
                {
                    properties.Add(key, loggingEvent.Properties[key]);
                }
            }

            _hecSender.Send(
                loggingEvent.TimeStampUtc,
                null,
                loggingEvent.Level.Name,
                null,
                loggingEvent.RenderedMessage,
                loggingEvent.ExceptionObject,
                properties,
                metaData
                );

            _hecSender.FlushSync();
        }
Beispiel #5
0
        /// <summary>
        /// Method used by log4net to perform actual logging
        /// </summary>
        /// <param name="loggingEvent"></param>
        protected override void Append(LoggingEvent loggingEvent)
        {
            // Sanity check for LogEventInfo
            if (loggingEvent == null)
            {
                throw new ArgumentNullException(nameof(loggingEvent));
            }

            // Make sure we have a properly setup HttpEventCollectorSender
            if (_hecSender == null)
            {
                throw new Exception("SplunkHttpEventCollector Append() called before ActivateOptions()");
            }

            // Build metaData
            var metaData = new HttpEventCollectorEventInfo.Metadata(Index, loggingEvent.LoggerName, "_json", GetMachineName());

            // Build properties object and assign standard values
            var properties = new Dictionary <String, object>
            {
                { "Source", loggingEvent.LoggerName },
                { "Host", GetMachineName() }
            };

            // Get properties from event
            if (loggingEvent.Properties != null && loggingEvent.Properties.Count > 0)
            {
                foreach (var key in loggingEvent.Properties.GetKeys())
                {
                    properties.Add(key, loggingEvent.Properties[key]);
                }
            }

            // Send the event to splunk
            _hecSender.Send(loggingEvent.TimeStampUtc, null, loggingEvent.Level.Name, null, loggingEvent.RenderedMessage, loggingEvent.ExceptionObject, properties, metaData);
            // the sync waiting will lock the code execution and break batch processing
            //_hecSender.FlushSync();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="loggingEvent"></param>
        private void SendEventToServer(LoggingEvent loggingEvent)
        {
            // Sanity check for LogEventInfo
            if (loggingEvent == null)
            {
                throw new ArgumentNullException(nameof(loggingEvent));
            }

            // Make sure we have a properly setup HttpEventCollectorSender
            if (_hecSender == null)
            {
                throw new Exception("SplunkHttpEventCollector SendEventToServer() called before InitializeTarget()");
            }

            // Build metaData
            var metaData = new HttpEventCollectorEventInfo.Metadata(null, loggingEvent.LoggerName, SourceType, GetMachineName());

            // Build properties object
            var properties = new Dictionary <String, object>();

            // Add standard values to properties
            properties.Add("Source", loggingEvent.LoggerName);
            properties.Add("Host", GetMachineName());

            // Get properties from event
            if (loggingEvent.Properties != null && loggingEvent.Properties.Count > 0)
            {
                foreach (var key in loggingEvent.Properties.GetKeys())
                {
                    properties.Add(key, loggingEvent.Properties[key]);
                }
            }

            // Send the event to splunk
            _hecSender.Send(null, loggingEvent.Level.Name, null, loggingEvent.RenderedMessage, loggingEvent.ExceptionObject, properties, metaData);
            _hecSender.FlushSync();
        }