This post is about using Slack (“team communication for the 21st century team”) as a logging tool with ServiceStack. Rather than using the conventional approach of logging to a text file or (even worse) the Windows Events approach, I will use Slack for logging such that Log.Info(“This is magic”) will post a Slack message to the relevant Slack channel & using Slack’s awesome tooling, all participants are immediately notified.
What is Slack?
If you are reading this & you work in a team then you should be using Slack. Period. For more convincing arguments, see the Quora post here on What makes Slack a must use Product. The important points are, tt’s free and will revolutionize the way you interact with your colleagues. What really differentiates this product from others (such as Skype), is the ease of integrating third party services (like GitHub, Asana, Google and/or custom integrations (as we will see in this post)) to post messages directly to Slack channels. This opens the door in so many ways different ways .
Slack as a Logging Tool!?! Is it not a chat app?!
So is it weird for Slack to be used as a Logging tool?! Not really, if you consider the most widely used practice for logging is to a random text file somewhere on the server. In reality this means getting a call from your boss (or you making a call, gasp!) at 1am, spending valuable sleep time getting onto the server, finding the error log & then realising you have load-balancing in place, which means it is on another server (this is something that really happened! Argh no jokes). No thanks, I choose the Slack app for firstly [turning message notificatons on] for the really, really important stuff (like high level service exceptions) and [turning off] notifications for any other stuff (I value my sleep, you know).
Ok, I am convinced. How do I implement Slack with ServiceStack?
ServiceStack provides an implementation-free ILog and ILogFactory interface that our application logic can bind to. This allows us to create our own custom logger (the SlackLogger) easily. Thus when we call Log.Info(“Magic was used to post this message to Slack”), it is sent to our Team’s slack channel and all channel participants are notified immediately.
A. Setting up Slack
We will need to set up Slack in order to get our custom webhook url, which is the ‘key’ to post Slack notifications.
- Go to https://api.slack.com/ and Register as a developer (believe me, this is very very quick!)
- Then click on ‘Start building custom integrations’
3. Click on [Set up incoming webhooks]
4. Sign into your relevant team & either select an existing / default channel you would like to post to or create a new channel. [Note even though this is the default channel, you are able to send messages to other channels using the same webhook]
5. Copy the Webhook URL
Posting to Slack with our custom SlackLogger