Пример #1
0
        public LogCastClient([NotNull] LogCastOptions options, [NotNull] IFallbackLogger fallbackLogger)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (fallbackLogger == null)
            {
                throw new ArgumentNullException(nameof(fallbackLogger));
            }

            if (options.Endpoint == null)
            {
                fallbackLogger.Write("Endpoint cannot be null reference");
                throw new ArgumentException("Endpoint can not be null reference", nameof(options));
            }
            if (options.Throttling < 10)
            {
                fallbackLogger.Write("Throttling cannot be less than 10");
                throw new ArgumentException("Throttling can not be less than 10", nameof(options));
            }

            _options        = options;
            _fallbackLogger = fallbackLogger;
            _queue          = new BlockingCollection <LogCastMessageFactory>();

            var sendingThreadCount = _options.SendingThreadCount;
            var threadCount        = sendingThreadCount > 0 ? sendingThreadCount : 4;

            _countEvent = new CountEvent();
            Threads     = Enumerable
                          .Range(0, threadCount)
                          .Select(i => new Thread(Sender)
            {
                IsBackground = true
            })
                          .ToArray();

            foreach (var thread in Threads)
            {
                thread.Start();
            }
        }
Пример #2
0
        private void Sender()
        {
            var threadId = Thread.CurrentThread.ManagedThreadId;

            var timeoutMsec = (int)_options.SendTimeout.TotalMilliseconds;
            int dropCount   = 0;
            int retryCount  = 0;

            foreach (var messageFactory in _queue.GetConsumingEnumerable())
            {
                try
                {
                    if (ConsumeDocument(threadId, messageFactory, timeoutMsec, dropCount, retryCount, out var newRetries))
                    {
                        dropCount  = 0;
                        retryCount = 0;
                    }
                    else
                    {
                        ++dropCount;
                    }
                    retryCount += newRetries;
                }
                catch (Exception ex)
                {
                    try
                    {
                        var queueLength = _queue.Count;
                        _fallbackLogger.Write(ex,
                                              "UNHANDLED ERROR",
                                              Stats(threadId, ++dropCount, retryCount, queueLength));
                    }
                    catch
                    {
                    }
                }
                finally
                {
                    _countEvent.Decrease();
                }
            }
        }
Пример #3
0
        public void Log(string loggerName, LogLevel level, string message, Exception exception, LogProperty[] properties)
        {
            if (!IsLevelEnabled(level))
            {
                return;
            }

            try
            {
                var logMessage = CreateMessage(loggerName, level, message, exception, properties);
                _messageRouter.DispatchMessage(logMessage);
            }
            catch (Exception ex)
            {
                _fallbackLogger?.Write(ex,
                                       $"Failed to dispatch a message. Original message: [{loggerName}] {message}");
            }
        }