Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferFlushingTask{TBufferItem, TMessage}" /> class.
 /// </summary>
 /// <param name="messageQueue">The queue message for the instance <see cref="BufferWithEviction{TIn}" /> </param>
 /// <param name="log">The log service.</param>
 protected BufferFlushingTask(BufferWithEviction <TBufferItem> messageQueue, ILog log)
 {
     this.MessageQueue  = messageQueue;
     this.LastFlushedOn = DateTime.UtcNow;
     this.Log           = log ?? new DummyLog();
     this.IsFlushing    = false;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DummyBufferFlushingTask" /> class.
 /// </summary>
 /// <param name="queue">The queue message for the instance <see cref="BufferWithEviction{TIn}" /> </param>
 /// <param name="maxFlushInterval">The maximum interval for flushing.</param>
 /// <param name="messagesPerRequest">The maximum messages per request.</param>
 /// <param name="name">The name.</param>
 public DummyBufferFlushingTask(BufferWithEviction <string> queue, TimeSpan maxFlushInterval, long messagesPerRequest, string name)
     : this(queue)
 {
     this.MaxFlushInterval   = maxFlushInterval;
     this.MessagesPerRequest = messagesPerRequest;
     this.MessagesName       = name;
 }
        private void InitBuffer(LoggerOptions options)
        {
            DebuggingLogger?.Debug("InitBuffer");

            messagesQueue = new BufferWithFifoEviction <string>(
                options.MaxQueueSizeBytes,
                new StringLengthCostAssigner(),
                DebuggingLogger);

            flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                messagesQueue,
                sumoLogicMessageSender,
                options.MaxFlushInterval,
                options.MessagesPerRequest,
                options.SourceName,
                options.SourceCategory,
                options.SourceHost,
                DebuggingLogger);

            flushBufferTimer = new Timer(
                callback: _ => flushBufferTask.Run(), // No task await to avoid unhandled exception
                state: null,
                dueTime: TimeSpan.FromMilliseconds(0),
                period: options.FlushingAccuracy);

            DebuggingLogger?.Debug("InitBuffer::Completed");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SumoLogicSink"/> class.
        /// </summary>
        /// <param name="log">The log service.</param>
        /// <param name="httpMessageHandler">HTTP message handler.</param>
        /// <param name="connection">Connection configuration.</param>
        /// <param name="source">Event source describer.</param>
        /// <param name="formatter">Text formatter.</param>
        public SumoLogicSink(
            ILog log,
            HttpMessageHandler httpMessageHandler,
            SumoLogicConnection connection,
            SumoLogicSource source,
            ITextFormatter formatter)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
            _log       = log ?? new DummyLog();

            _messageSender = MessageSenderFactory.CreateMessageSender(_log, httpMessageHandler, connection);
            _messageQueue  = MessageQueueFactory.CreateMessageQueue(_log, connection);
            SumoLogicMessageSenderBufferFlushingTask flushBufferTask = FlushBufferTaskFactory.CreateFlushBufferTask(
                _log,
                _messageSender,
                _messageQueue,
                connection,
                source);

            _flushBufferTimer = new Timer(
                _ => flushBufferTask.Run(),
                null,
                TimeSpan.FromMilliseconds(0),
                connection.FlushingAccuracy);
        }
        /// <summary>
        /// Initialize the target based on the options set
        /// </summary>
        /// <remarks>
        /// This is part of the nLog is called when the Configurations of the LogManager are set.
        /// If any of the configuration properties are modified then you must set anew the Configuration of the LogManager.
        /// </remarks>
        protected override void InitializeTarget()
        {
            if (this.UseConsoleLog)
            {
                this.ActivateConsoleLog();
            }

            if (this.LogLog.IsDebugEnabled)
            {
                this.LogLog.Debug("Activating options");
            }

            // Initialize the messages queue
            if (this.messagesQueue == null)
            {
                this.messagesQueue = new BufferWithFifoEviction <string>(this.MaxQueueSizeBytes, new StringLengthCostAssigner(), this.LogLog);
            }
            else
            {
                this.messagesQueue.Capacity = this.MaxQueueSizeBytes;
            }

            // Initialize the sender
            if (this.SumoLogicMessageSender == null)
            {
                this.SumoLogicMessageSender = new SumoLogicMessageSender(this.HttpMessageHandler, this.LogLog);
            }

            this.SumoLogicMessageSender.RetryInterval     = TimeSpan.FromMilliseconds(this.RetryInterval);
            this.SumoLogicMessageSender.ConnectionTimeout = TimeSpan.FromMilliseconds(this.ConnectionTimeout);
            this.SumoLogicMessageSender.Url = string.IsNullOrEmpty(this.Url) ? null : new Uri(this.Url);

            // Initialize flusher
            if (this.flushBufferTimer != null)
            {
                this.flushBufferTimer.Stop();
                this.flushBufferTimer.Dispose();
            }

            // Ensure any existing buffer is flushed
            if (this.flushBufferTask != null)
            {
                this.flushBufferTask.FlushAndSend();
            }

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messagesQueue,
                this.SumoLogicMessageSender,
                TimeSpan.FromMilliseconds(this.MaxFlushInterval),
                this.MessagesPerRequest,
                this.SourceName,
                this.LogLog);

            this.flushBufferTimer          = new Timer(TimeSpan.FromMilliseconds(this.FlushingAccuracy).TotalMilliseconds);
            this.flushBufferTimer.Elapsed += (s, e) => this.flushBufferTask.Run();

            this.flushBufferTimer.Start();
        }
        /// <summary>
        /// Initialize the appender based on the options set
        /// </summary>
        /// <remarks>
        /// This is part of the log4net.Core.IOptionHandler delayed object activation scheme. The ActivateOptions() method must be called
        /// on this object after the configuration properties have been set. Until ActivateOptions() is called this object is in an undefined
        /// state and must not be used. If any of the configuration properties are modified then ActivateOptions() must be called again.
        /// </remarks>
        public override void ActivateOptions()
        {
            if (this.UseConsoleLog)
            {
                this.ActivateConsoleLog();
            }

            if (this.LogLog.IsDebugEnabled)
            {
                this.LogLog.Debug("Activating options");
            }

            // Initialize the messages queue
            if (this.messagesQueue == null)
            {
                this.messagesQueue = new BufferWithFifoEviction <string>(this.MaxQueueSizeBytes, new StringLengthCostAssigner(), this.LogLog);
            }
            else
            {
                this.messagesQueue.Capacity = this.MaxQueueSizeBytes;
            }

            // Initialize the sender
            if (this.SumoLogicMessageSender == null)
            {
                this.SumoLogicMessageSender = new SumoLogicMessageSender(this.HttpMessageHandler, this.LogLog, "sumo-log4net-buffered-sender");
            }

            this.SumoLogicMessageSender.RetryInterval     = TimeSpan.FromMilliseconds(this.RetryInterval);
            this.SumoLogicMessageSender.ConnectionTimeout = TimeSpan.FromMilliseconds(this.ConnectionTimeout);
            this.SumoLogicMessageSender.Url = string.IsNullOrEmpty(this.Url) ? null : new Uri(this.Url);

            // Initialize flusher
            if (this.flushBufferTimer != null)
            {
                this.flushBufferTimer.Dispose();
            }

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messagesQueue,
                this.SumoLogicMessageSender,
                TimeSpan.FromMilliseconds(this.MaxFlushInterval),
                this.MessagesPerRequest,
                this.SourceName,
                this.SourceCategory,
                this.SourceHost,
                this.LogLog);

//            this.flushBufferTimer = new Timer(
//                _ => flushBufferTask.Run(), // No task await to avoid unhandled exception
//                null,
//                TimeSpan.FromMilliseconds(0),
//                TimeSpan.FromMilliseconds(this.FlushingAccuracy));

            this.flushBufferWorker = Task.Factory.StartNew(FlushBufferAction, CancellationToken.None, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initialize the appender based on the options set
        /// </summary>
        /// <remarks>
        /// This is part of the log4net.Core.IOptionHandler delayed object activation scheme. The ActivateOptions() method must be called
        /// on this object after the configuration properties have been set. Until ActivateOptions() is called this object is in an undefined
        /// state and must not be used. If any of the configuration properties are modified then ActivateOptions() must be called again.
        /// </remarks>
        public override void ActivateOptions()
        {
            if (this.UseConsoleLog)
            {
                this.ActivateConsoleLog();
            }

            if (this.LogLog.IsDebugEnabled)
            {
                this.LogLog.Debug("Activating options");
            }

            // Initialize the messages queue
            if (this.messagesQueue == null)
            {
                this.messagesQueue = new BufferWithFifoEviction <string>(this.MaxQueueSizeBytes, new StringLengthCostAssigner(), this.LogLog);
            }
            else
            {
                this.messagesQueue.Capacity = this.MaxQueueSizeBytes;
            }

            // Initialize the sender
            if (this.SumoLogicMessageSender == null)
            {
                this.SumoLogicMessageSender = new SumoLogicMessageSender(this.HttpMessageHandler, this.LogLog, "sumo-log4net-buffered-sender");
            }

            this.SumoLogicMessageSender.RetryInterval     = TimeSpan.FromMilliseconds(this.RetryInterval);
            this.SumoLogicMessageSender.ConnectionTimeout = TimeSpan.FromMilliseconds(this.ConnectionTimeout);
            this.SumoLogicMessageSender.Url = string.IsNullOrEmpty(this.Url) ? null : new Uri(this.Url);

            // Initialize flusher
            if (this.flushBufferTimer != null)
            {
                this.flushBufferTimer.Dispose();
            }

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messagesQueue,
                this.SumoLogicMessageSender,
                TimeSpan.FromMilliseconds(this.MaxFlushInterval),
                this.MessagesPerRequest,
                this.SourceName,
                this.SourceCategory,
                this.SourceHost,
                this.LogLog);

            this.flushBufferTimer = new Timer(
                async _ => await flushBufferTask.Run().ConfigureAwait(false),
                null,
                TimeSpan.FromMilliseconds(0),
                TimeSpan.FromMilliseconds(this.FlushingAccuracy));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SumoLogicMessageSenderBufferFlushingTask" /> class.
 /// </summary>
 /// <param name="messagesQueue">The queue message for the instance <see cref="BufferWithEviction{TIn}" /> </param>
 /// <param name="messageSender">The http sender.</param>
 /// <param name="maxFlushInterval">The maximum interval for flushing.</param>
 /// <param name="messagesPerRequest">The maximum messages per request.</param>
 /// <param name="messagesName">The messages name.</param>
 /// <param name="log">The log service.</param>
 public SumoLogicMessageSenderBufferFlushingTask(
     BufferWithEviction <string> messagesQueue,
     SumoLogicMessageSender messageSender,
     TimeSpan maxFlushInterval,
     long messagesPerRequest,
     string messagesName,
     ILog log)
     : base(messagesQueue, log)
 {
     this.MaxFlushInterval   = maxFlushInterval;
     this.MessagesPerRequest = messagesPerRequest;
     this.MessagesName       = messagesName;
     this.MessageSender      = messageSender;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BufferedSumoLogicSink"/> class.
        /// </summary>
        /// <param name="log">The log service.</param>
        /// <param name="httpMessageHandler">HTTP message handler.</param>
        /// <param name="connection">Connection configuration.</param>
        /// <param name="source">Event source describer.</param>
        /// <param name="formatter">Text formatter.</param>
        public BufferedSumoLogicSink(
            ILog log,
            HttpMessageHandler httpMessageHandler,
            SumoLogicConnection connection,
            SumoLogicSource source,
            ITextFormatter formatter)
        {
            if (connection is null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            this.formatter  = formatter ?? throw new ArgumentNullException(nameof(formatter));
            this.logService = log ?? new DummyLog();

            this.messageSender = new SumoLogicMessageSender(httpMessageHandler, this.logService)
            {
                Url               = connection.Uri,
                ClientName        = connection.ClientName,
                ConnectionTimeout = connection.ConnectionTimeout,
                RetryInterval     = connection.RetryInterval,
            };

            this.messageQueue = new BufferWithFifoEviction <string>(
                connection.MaxQueueSizeBytes,
                new StringLengthCostAssigner(),
                this.logService);

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messageQueue,
                this.messageSender,
                connection.MaxFlushInterval,
                connection.MessagesPerRequest,
                source.SourceName,
                source.SourceCategory,
                source.SourceHost,
                this.logService);

            this.flushBufferTimer = new Timer(
                _ => flushBufferTask.Run(), // No task await to avoid unhandled exception
                null,
                TimeSpan.FromMilliseconds(0),
                connection.FlushingAccuracy);
        }
 public static SumoLogicMessageSenderBufferFlushingTask CreateFlushBufferTask(
     ILog log,
     SumoLogicMessageSender messageSender,
     BufferWithEviction <string> messageQueue,
     SumoLogicConnection connection,
     SumoLogicSource source) =>
 new SumoLogicMessageSenderBufferFlushingTask(
     messageQueue,
     messageSender,
     connection.MaxFlushInterval,
     connection.MessagesPerRequest,
     source.SourceName,
     source.SourceCategory,
     source.SourceHost,
     log);
 public SumoLogicMessageSenderBufferFlushingTask(
     BufferWithEviction <string> messagesQueue,
     SumoLogicMessageSender messageSender,
     TimeSpan maxFlushInterval,
     long messagesPerRequest,
     string messagesName,
     ILog log)
     : this(messagesQueue,
            messageSender,
            maxFlushInterval,
            messagesPerRequest,
            messagesName,
            null,
            null,
            log)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DummyBufferFlushingTask" /> class.
 /// </summary>
 /// <param name="queue">The queue.</param>
 public DummyBufferFlushingTask(BufferWithEviction <string> queue)
     : base(queue, null)
 {
     this.SentOut = new List <List <string> >();
 }
        /// <summary>
        /// Initialize the target based on the options set
        /// </summary>
        /// <remarks>
        /// This is part of the nLog is called when the Configurations of the LogManager are set.       
        /// If any of the configuration properties are modified then you must set anew the Configuration of the LogManager. 
        /// </remarks>
        protected override void InitializeTarget()
        {
            if (this.UseConsoleLog)
            {
                this.ActivateConsoleLog();
            }

            if (this.LogLog.IsDebugEnabled)
            {
                this.LogLog.Debug("Activating options");
            }

            // Initialize the messages queue
            if (this.messagesQueue == null)
            {
                this.messagesQueue = new BufferWithFifoEviction<string>(this.MaxQueueSizeBytes, new StringLengthCostAssigner(), this.LogLog);
            }
            else
            {
                this.messagesQueue.Capacity = this.MaxQueueSizeBytes;
            }

            // Initialize the sender
            if (this.SumoLogicMessageSender == null)
            {
                this.SumoLogicMessageSender = new SumoLogicMessageSender(this.HttpMessageHandler, this.LogLog);
            }

            this.SumoLogicMessageSender.RetryInterval = TimeSpan.FromMilliseconds(this.RetryInterval);
            this.SumoLogicMessageSender.ConnectionTimeout = TimeSpan.FromMilliseconds(this.ConnectionTimeout);
            this.SumoLogicMessageSender.Url = string.IsNullOrEmpty(this.Url) ? null : new Uri(this.Url);

            // Initialize flusher
            if (this.flushBufferTimer != null)
            {
                this.flushBufferTimer.Stop();
                this.flushBufferTimer.Dispose();
            }

            // Ensure any existing buffer is flushed
            if (this.flushBufferTask != null)
            {
                this.flushBufferTask.FlushAndSend();
            }

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messagesQueue,
                this.SumoLogicMessageSender,
                TimeSpan.FromMilliseconds(this.MaxFlushInterval),
                this.MessagesPerRequest,
                this.SourceName,
                this.LogLog);

            this.flushBufferTimer = new Timer(TimeSpan.FromMilliseconds(this.FlushingAccuracy).TotalMilliseconds);
            this.flushBufferTimer.Elapsed += (s, e) => this.flushBufferTask.Run();

            this.flushBufferTimer.Start();
        }
Exemplo n.º 14
0
        /// <summary>
        /// Initialize the target based on the options set
        /// </summary>
        /// <remarks>
        /// This is part of the nLog is called when the Configurations of the LogManager are set.
        /// If any of the configuration properties are modified then you must set anew the Configuration of the LogManager.
        /// </remarks>
        protected override void InitializeTarget()
        {
            if (this.UseConsoleLog)
            {
                this.ActivateConsoleLog();
            }

            if (this.LogLog.IsDebugEnabled)
            {
                this.LogLog.Debug("Activating options");
            }

            // Initialize the messages queue
            if (this.messagesQueue == null)
            {
                this.messagesQueue = new BufferWithFifoEviction <string>(this.MaxQueueSizeBytes, new StringLengthCostAssigner(), this.LogLog);
            }
            else
            {
                this.messagesQueue.Capacity = this.MaxQueueSizeBytes;
            }

            // Initialize the sender
            if (this.SumoLogicMessageSender == null)
            {
                this.SumoLogicMessageSender = new SumoLogicMessageSender(this.HttpMessageHandler, this.LogLog, "sumo-nlog-buffered-sender");
            }

            var url            = _urlLayout?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
            var sourceName     = _sourceLayout?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
            var sourceCategory = _categoryLayout?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
            var sourceHost     = _hostLayout?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;

            this.SumoLogicMessageSender.RetryInterval     = TimeSpan.FromMilliseconds(this.RetryInterval);
            this.SumoLogicMessageSender.ConnectionTimeout = TimeSpan.FromMilliseconds(this.ConnectionTimeout);
            this.SumoLogicMessageSender.Url = string.IsNullOrEmpty(url) ? null : new Uri(url);

            // Initialize flusher
            if (this.flushBufferTimer != null)
            {
                this.flushBufferTimer.Dispose();
            }

            // Ensure any existing buffer is flushed
            if (this.flushBufferTask != null)
            {
                // this is NOT present in the log4net code. one-time blocking operation.
                try
                {
                    this.flushBufferTask.FlushAndSend().GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    this.LogLog.Warn($"Buffered target failed to flush pending logevents. {ex.GetType()}: {ex.Message}");
                }
            }

            this.flushBufferTask = new SumoLogicMessageSenderBufferFlushingTask(
                this.messagesQueue,
                this.SumoLogicMessageSender,
                TimeSpan.FromMilliseconds(this.MaxFlushInterval),
                this.MessagesPerRequest,
                sourceName,
                sourceCategory,
                sourceHost,
                this.LogLog);

            this.flushBufferTimer = new Timer(
                _ => flushBufferTask.Run(), // No task await to avoid unhandled exception
                null,
                TimeSpan.FromMilliseconds(0),
                TimeSpan.FromMilliseconds(this.FlushingAccuracy));
        }