/// <summary> /// Sends information to Slack. You can disable this with Slack:DisableNotifications and make exceptions bubble to client caller with Slack:ThrowExceptions /// </summary> /// <param name="level"></param> /// <param name="location"></param> /// <param name="message"></param> /// <returns></returns> public bool Notify(SlackMessageLevel level, string location, string message) { var shouldDisableNotifications = ConfigurationManager.AppSettings["Slack:DisableNotifications"] == "true"; if (string.IsNullOrEmpty(_endpointUrl) || shouldDisableNotifications) { return(false); } try { var msg = _messageFactory.CreateMessage(level, _sender, location, message); var json = _messageFactory.ToJson(msg); return(_httpClient.Post(_endpointUrl, json)); } catch (Exception) { var shouldThrowException = ConfigurationManager.AppSettings["Slack:ThrowExceptions"] == "true"; if (shouldThrowException) { throw; } return(false); } }
/// <summary> /// Create a Slack message /// </summary> /// <param name="level"></param> /// <param name="sender"></param> /// <param name="location"></param> /// <param name="message"></param> /// <returns></returns> public SlackMessage CreateMessage(SlackMessageLevel level, string sender, string location, string message) { return(new SlackMessage { Username = sender, Attachments = new [] { new SlackMessage.Attachment { Text = $"{level} {location}:\n {message}", Color = MessageLevelColors[level], } } }); }