Using Slack for Logging (with ServiceStack) – Part 1

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.

loggerInfoToSlack.png

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.

  1. Go to https://api.slack.com/ and Register as a developer (believe me, this is very very quick!)
  2. Then click on ‘Start building custom integrations’

Screen Shot 2016-02-01 at 7.45.12 AM.png

3. Click on [Set up incoming webhooks]

Screen Shot 2016-02-01 at 7.47.55 AM

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]

Screen Shot 2016-02-01 at 7.49.36 AM.png

 

5. Copy the Webhook URL

Screen Shot 2016-02-01 at 7.51.53 AM.png

Posting to Slack with our custom SlackLogger

We will need to create a class ‘SlackLogger’ that inherits from ILog.
Then, we only need to remember two important points.
Firstly, the  incomingWebHookUrl (this is the Slack ‘key’ we got above).
Secondly, the line incomingWebHookUrl.PostToJsonUrl( new { text = message.ToString()}); 

incomingWebHookUrl.PostToJsonUrl( new { text = “random message });
I love simplicity of this one line allowed by ServiceStack’s HttpUtils. Especially when we compare this to the .NET approach of creating proxies and abstraction classes to wrap the WebRequest (ugly).
With this in place & making some messages more informative (e.g preceding info logs with “Info:”, we can now register this new SlackLogger in our app.
Registering SlackLogger in our App using IoC

Using it in your app:
Given that we have registered in our AppHost class, we are now able to use IoC in the services that need it, by simply instantiating it with public ILog Logger {get;set;} and then calling the relevant method such as Log.Info(“This is magic”) which will send a message to Slack.
Screen Shot 2016-02-01 at 8.46.08 AM.png

Summary
We have shown above how to set up Slack for a custom integration.
We have implemented our own SlackLog (ILog) implementation & registered it in our Application. Using Funq as the IoC container, we are able to call it in any service & send Slack notification messages.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s