Beispiel #1
0
        /// <summary>
        /// Overrides <see cref="PeriodicBatchingSink.EmitBatchAsync"/> and sends the <see cref="LogEvent"/> with a <see cref="SlackClient"/> to Slack.
        /// </summary>
        /// <param name="events">A collection of <see cref="LogEvent"/>.</param>
        /// <returns>An Awaitable Task.</returns>
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            // check activation status
            if (_slackSinkActivationSwitch.SlackSinkStatus == SlackSinkActivationSwitch.SlackSinkActivationStatus.InActive)
            {
                return;
            }

            foreach (LogEvent logEvent in events)
            {
                // create new slack message
                var msg = new SlackMessage
                {
                    Attachments     = _generateSlackMessageAttachments(logEvent, _formatProvider, _slackSinkOptions),
                    Blocks          = _generateSlackMessageBlocks(logEvent, _formatProvider, _slackSinkOptions),
                    Channel         = _slackSinkOptions.SlackChannels != null && _slackSinkOptions.SlackChannels.Any() ? _slackSinkOptions.SlackChannels[0] : null,
                    DeleteOriginal  = _slackSinkOptions.SlackDeleteOriginal,
                    IconEmoji       = _slackSinkOptions.SlackEmojiIcon,
                    IconUrl         = _slackSinkOptions.SlackUriIcon,
                    LinkNames       = _slackSinkOptions.SlackLinkNames,
                    Markdown        = _slackSinkOptions.SlackMarkdown,
                    Parse           = _slackSinkOptions.SlackParse,
                    ReplaceOriginal = _slackSinkOptions.SlackReplaceOriginal,
                    ResponseType    = _slackSinkOptions.SlackResponseType,
                    Text            = _generateSlackMessageText(logEvent, _formatProvider, _slackSinkOptions),
                    ThreadId        = _slackSinkOptions.SlackThreadId,
                    Username        = _slackSinkOptions.SlackUsername
                };

                // check for multi channel post
                if (_slackSinkOptions.SlackChannels != null)
                {
                    // multi channel post
                    var logMsgPosts = _slackClient.PostToChannelsAsync(msg, _slackSinkOptions.SlackChannels);

                    foreach (Task <bool> logMsgPost in logMsgPosts)
                    {
                        await logMsgPost;
                    }
                }
                else
                {
                    // single channel post
                    await _slackClient.PostAsync(msg);
                }
            }
        }