A worker service is a type of Background Service that are generally use for long-running task. They can be seen as the equivalent of Windows Services in the .NET Framework, though a worker service is not limited to just windows.
If you are building a worker service, then more than likely you will need to be able to write log data, be that general information of the worker services or perhaps just errors. If you plan to use Serilog, then this post will show you how to configure Serilog on a worker project.
To configure Serilog you will need the following NuGet packages.
Once the packages are installed modify the Program.cs file to bootstrap Serilog and to confiure Serilog. The following class illustrates how I usually configure Serilog.
The most important piece of code in this class is the usage of the method UserSerilog. Withouth it you will have a configured Serilog but it won’t be used by the worker, so don’t forgot to use it. As for the method ConfigureSerilog, that is one of my extension methods. It reads Serilog configurations from appsettings.json. Here is the class definition.