/// <summary> /// Adds a sink that writes log events to a channel in Slack. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="channels">List of Slack channels.</param> /// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="formatProvider">FormatProvider to apply in <see cref="LogEvent.RenderMessage(IFormatProvider)"/>. It overrides default behaviour.</param> /// <param name="username">Optional bot name</param> /// <param name="iconUrl">Optional URL to an image to use as the icon for this message.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration Slack( this LoggerSinkConfiguration loggerConfiguration, SlackChannelCollection channels, SlackSink.RenderMessageMethod renderMessageImplementation = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IFormatProvider formatProvider = null, string username = null, string iconUrl = null ) { if (loggerConfiguration == null) { throw new ArgumentNullException(nameof(loggerConfiguration)); } if (channels == null) { throw new ArgumentNullException(nameof(channels)); } if (channels.Count == 0) { throw new ArgumentException("Must have at least one Slack channel defined."); } return(loggerConfiguration.Sink( new SlackSink( channels, renderMessageImplementation, formatProvider, username, iconUrl ), restrictedToMinimumLevel)); }
/// <summary> /// Adds a sink that writes log events to a channel in Slack. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="webhookUri">WebHook Uri that allows Slack Incoming Webhooks (https://api.slack.com/incoming-webhooks).</param> /// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="formatProvider">FormatProvider to apply in <see cref="LogEvent.RenderMessage(IFormatProvider)"/>. It overrides default behaviour.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration Slack( this LoggerSinkConfiguration loggerConfiguration, string webhookUri, SlackSink.RenderMessageMethod renderMessageImplementation = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IFormatProvider formatProvider = null ) { if (loggerConfiguration == null) { throw new ArgumentNullException("loggerConfiguration"); } if (string.IsNullOrWhiteSpace(webhookUri)) { throw new ArgumentNullException("webhookUri"); } return(loggerConfiguration.Sink( new SlackSink( webhookUri, renderMessageImplementation, formatProvider ), restrictedToMinimumLevel)); }
/// <summary> /// Construct a sink posting to the specified Slack Channel. /// </summary> /// <param name="webhookUri">WebHook Uri that allows Slack Incoming Webhooks (https://api.slack.com/incoming-webhooks).</param> /// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook. By default uses <see cref="RenderMessage"/>.</param> /// <param name="formatProvider">FormatProvider to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.</param> public SlackSink(string webhookUri, SlackSink.RenderMessageMethod renderMessageImplementation, IFormatProvider formatProvider) { if (string.IsNullOrWhiteSpace(webhookUri)) { throw new ArgumentNullException("webhookUri"); } FormatProvider = formatProvider; Channels.Add(new SlackChannel(webhookUri)); RenderMessageImplementation = renderMessageImplementation ?? RenderMessage; if (Channels.Count == 0) { SelfLog.WriteLine("There are 0 Slack channels defined. Slack sink will not send messages."); } }
/// <summary> /// Construct a sink posting to the specified Slack Channel. /// </summary> /// <param name="channels">Slack Channel list.</param> /// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook. By default uses <see cref="RenderMessage"/>.</param> /// <param name="formatProvider">FormatProvider to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.</param> /// <param name="username">Optional bot name</param> /// <param name="iconUrl">Optional URL to an image to use as the icon for this message.</param> public SlackSink(SlackChannelCollection channels, SlackSink.RenderMessageMethod renderMessageImplementation, IFormatProvider formatProvider, string username = null, string iconUrl = null) { if (channels == null) { throw new ArgumentNullException("channels"); } FormatProvider = formatProvider; Channels = channels; RenderMessageImplementation = renderMessageImplementation ?? RenderMessage; _username = username; _iconUrl = iconUrl; if (Channels.Count == 0) { SelfLog.WriteLine("There are 0 Slack channels defined. Slack sink will not send messages."); } }