Esempio n. 1
0
        /// <inheritdoc />
        public void Notify([NotNull] string healthMessage, [CanBeNull] object context)
        {
            var sender = $":loudspeaker: {_appName} {_appVersion} : {_envInfo}";

            var messageBuilder = new StringBuilder();

            messageBuilder.Append(healthMessage);

            if (context != null)
            {
                messageBuilder.AppendLine();
                messageBuilder.Append(LogContextConversion.ConvertToString(context));
            }

            var message = messageBuilder.ToString();
            var tasks   = new List <Task> {
                _slackSender.SendMonitorAsync(message, sender)
            };

            tasks.AddRange(_customChannels.Select(c => _slackSender.SendAsync(c, sender, message)));

            Task.WhenAll(tasks).ConfigureAwait(false).GetAwaiter().GetResult();
        }
        protected override async Task Consume(LogEntity entry)
        {
            try
            {
                if (!_logLevels.Contains(entry.Level))
                {
                    return;
                }

                var componentName = GetComponentName(entry);

                switch (entry.Level)
                {
                case LykkeLogToAzureStorage.FatalErrorType:
                {
                    var message = entry.Context != null
                            ? $"{entry.Msg} : {entry.Stack} : {entry.Context}"
                            : $"{entry.Msg} : {entry.Stack}";
                    await _slackNotificationsSender.SendErrorAsync(message, componentName);

                    break;
                }

                case LykkeLogToAzureStorage.ErrorType:
                {
                    if (await _spamGuard.ShouldBeMutedAsync(LogLevel.Error, componentName, entry.Process))
                    {
                        break;
                    }

                    var message = entry.Context != null
                            ? $"{entry.Msg} : {entry.Stack} : {entry.Context}"
                            : $"{entry.Msg} : {entry.Stack}";
                    await _slackNotificationsSender.SendErrorAsync(message, componentName);

                    break;
                }

                case LykkeLogToAzureStorage.WarningType:
                {
                    if (await _spamGuard.ShouldBeMutedAsync(LogLevel.Warning, componentName, entry.Process))
                    {
                        break;
                    }

                    var message = entry.Context != null
                            ? $"{entry.Msg} : {entry.Context}"
                            : entry.Msg;
                    await _slackNotificationsSender.SendWarningAsync(message, componentName);

                    break;
                }

                case LykkeLogToAzureStorage.MonitorType:
                {
                    if (await _spamGuard.ShouldBeMutedAsync(LogLevel.Monitoring, componentName, entry.Process))
                    {
                        break;
                    }

                    var message = entry.Context != null
                            ? $"{entry.Msg} : {entry.Context}"
                            : entry.Msg;
                    await _slackNotificationsSender.SendMonitorAsync(message, componentName);

                    break;
                }
                }
            }
            catch (Exception ex)
            {
                await _lastResortLog.WriteErrorAsync("Send log entries to the Slack", "", ex);
            }
        }