Push unhandled errors in an ASP.Net project directly into a Slack channel.
Setting up is nice and easy. First you need to get a URL from your Slack channel.
-
Open up the custom integration builder from your Slack app, the URL will be something like this:
https://<your-app-name>.slack.com/apps/build/custom-integration
-
Create a new Incoming WebHook.
-
Choose the channel you want to post to and click Add.
-
Note the WebHook URL, this is what you need in all the web applications that you want to post into this channel.
-
Install the Wired.SlackErrors library into your app by manually referencing the DLL or adding the Nuget package (adding to Nuget is a TODO for me!)
-
In your
web.config
file, you need to add the following line into theconfigSections
element. This registers the configuration element in the next step -
Add in the following configuration element:
<slackErrors appName="<website name>"> <channels> <add name="<channel name>" postUrl="<webhoook-url>" /> </channels> </slackErrors>
-
Make sure to replace the
appName
(this is posted into teh Slack channel so you can easily identify the error source), the channel name (this is only useful for you to identify where the URL is posting to) and thepostUrl
(as given above in the Slack configuration)
That's all that is needed. Now any unhandled errors (those pesky things that cause your ASP.Net application to show the yellow screen of death) will get posted to Slack.
It is possible to filter the errors that get posted, this is especially useful if you are only interested in specific error type or you're looking for specific text in the stack trace. You can filter on exception type or stack trace, for example this will ensure that only NullReferenceException
exceptions are sent to Slack:
<add name="Website Errors Channel"
postUrl="https://hooks.slack.com/services/..."
typeFilter="System\.NullReferenceException" />
If you want to filter on stack trace, use traceFilter
instead:
<add name="Website Errors Channel"
postUrl="https://hooks.slack.com/services/..."
traceFilter="......" />
Both traceFilter
and traceFilter
use regular expressions to match. So for example if you only wanted to see SqlException
and ApplicationException
exceptions, you can do this:
<add name="Website Errors Channel"
postUrl="https://hooks.slack.com/services/..."
typeFilter="System\.(Sql|Application)Exception" />
You can use filtering to post different exception types to different channels, so if you wanted all SqlException
in one channel and all ApplicationException
in another, just add two channels with specific filters:
<add name="Application Errors Channel"
postUrl="https://hooks.slack.com/services/...URL2"
typeFilter="System\.ApplicationException" />
<add name="SQL Errors Channel"
postUrl="https://hooks.slack.com/services/...URL1"
typeFilter="System\.SqlException" />