Slack Logging (Webhook) with Worker Service in .Net Core (C#)

Kemal AKCIL
4 min readDec 23, 2020

--

In this article, I’ll try to instill the idea of using Worker Service and Slack together, where you can perform scheduled and repetitive operations in .Net Core. You can simply create your Worker Service in .Net Core 3.0 with a command line or from the “Create a new Project” section. However, this is not mandatory! You can also use Worker Service in your MVC or API project etc.

with a command line
with a command line
with creating new project

In this walkthrough, I will create a Worker Service, include a Logger for Slack and run it. But I won’t publish it. By the way, It is also possible to publish for Linux Daemon or Windows Service.

After installation, we will see a background class named Worker.cs by default.

Worker.cs
Worker.cs

Now let’s examine the contents of our Worker.cs file. The contents of our file are given below. Worker.cs is inherited from an abstract class named BackgroundService. There are crushable methods called StartAsync, StopAsync and ExecuteAsync in this class.

BackgroundService
BackgroundService

It is not mandatory to use the StartAsync and StopAsync methods. When these methods are not used, the service will call the ExecuteAsync method to start running. We will not use these two non-mandatory methods as we will briefly mention it here. We will not use it, but if you want to install it as a Service, we need to install extra packages (it will depend on the broadcast environment) so that we can run it. All of these are in the picture below. You can install it using the NuGet.

necessary packages
necessary packages

In the next section, we will touch on the Slack side to perform logging. We will do logging using two methods. You don’t need to install a package for this. A Post request is sufficient.

We have limitations in terms of functionality of the Webhook method. However, these can be tackled, depending on your abilities. Let’s see how we create a Webhook. We create a new App from the address (https://api.slack.com/apps).

Create a Slack App
Create a Slack App

We give our application a name and choose a Workspace for our it.

Create a Slack App
Create a Slack App

We activate the Incoming Webhook from the page that comes up, and we get information to send a request. Thanks to this incoming data, the requests we make will be sent to Slack as a message. If you wish, you can test it with Postman or a similar tool before moving to the .NET side. We will not.

Post Request Details
Post Request Details

If you are going to publish for Windows Service or Linux, open the Program.cs file and add the .UseWindowsService () method to the part shown in the picture. Those using Linux will use .UseSystemd ().

However, as I said before, I do not go into it with much detail because I do not want to use it as a Service.

.UseWindowsService()
.UseWindowsService()

Let’s go to the Worker class, inherited from BackgroundService. In this class, we see the code that enables it to log to the console screen per second by default. Clean them up!

Thanks to the Webhook we just created, we configure it as shown in the picture, by applying the necessary post processes to make requests.

worker.cs

When we run the program, we see a console screen like the one below.

while running
notification from Slack

Thus, our request has been sent. You can also publish it as a Windows Service if you wish so that it runs continuously in the background. Or you can choose to use it wherever necessary in your .Net Core projects.

The choice is up to you.

--

--