예제 #1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            var variableValues = new Dictionary <string, object>
            {
                ["LogLevel"]      = logLevel,
                ["LogLevelSmall"] = GetLogLevelString(logLevel),
                ["DateTime"]      = DateTime.Now.ToString(_config.TimeStampFormat),
                ["EventId"]       = eventId.Id,
                ["Name"]          = _name,
                ["State"]         = formatter(state, exception),
                ["Exception"]     = exception?.ToString() ?? string.Empty
            };

            string message = StringTemplate.Render(
                logLevel < _config.LogToStandardErrorThreshold ? _config.LogFormat : _config.LogErrorFormat,
                variableValues);


            if (_config.Async)
            {
                _telegramMessageService.QueueMessage(new LogMessageEntry
                {
                    ChatId  = _config.ChatId,
                    Message = message.ToString()
                });

                if (logLevel >= _config.LogToStandardErrorThreshold && _config.LogErrorChatId != 0)
                {
                    _telegramMessageService.QueueMessage(new LogMessageEntry
                    {
                        ChatId  = _config.LogErrorChatId,
                        Message = message.ToString()
                    });
                }
            }
            else
            {
                _telegramMessageService.SendMessage(new LogMessageEntry
                {
                    ChatId  = _config.ChatId,
                    Message = message.ToString()
                }).GetAwaiter().GetResult();

                if (logLevel >= _config.LogToStandardErrorThreshold && _config.LogErrorChatId != 0)
                {
                    _telegramMessageService.SendMessage(new LogMessageEntry
                    {
                        ChatId  = _config.LogErrorChatId,
                        Message = message.ToString()
                    }).GetAwaiter().GetResult();
                }
            }
        }
예제 #2
0
        private async Task BackgroundProcessing(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var entry = await _taskQueue.DequeueAsync(stoppingToken);

                await _telegramMessageService.SendMessage(entry);
            }
        }