C#
Sam Lau  

Clean setup of Serilog with seq and Application Insights in .NET 8 Worker service

If you are looking for a no BS setup guide for Serilog with seq and Application Insights sink in .NET 8 worker services, you have came to the right place. Let’s get started!

Sample project

I have created a sample project in GitHub: samtc-lau/SerilogWorkerServicesSample: A sample .NET 8 Worker service application with Serilog (github.com). You can reference this project and follow along.

Seq

Seq is a self-hosted log server. You can use it for production but I mostly use it locally for development. The easiest way to set it up locally is with docker. Run this command copied from this link.

docker run --name seq -d --restart unless-stopped -e ACCEPT_EULA=Y -p 5341:80 datalust/seq:latest

Visit http://localhost:5341/ for viewing logs.

NuGut Pakages

Add the following packages:

  • Serilog
  • Serilog.Extensions.Hosting
  • Serilog.Settings.Configuration
  • Serilog.Sinks.ApplicationInsights
  • Serilog.Sinks.Console
  • Serilog.Sinks.Seq

Program.cs

Add this line to your Program.cs.

builder.Services.AddSerilog((serviceProvider, configuration) =>  
{  
    configuration.ReadFrom.Configuration(builder.Configuration);  
});

This is what I like about this setup. This is the only line you need to add to code. The other configurations sit in appsettings.json.

appsetting.json

The way I set up is that log will sink to seq and console in development and sink to Application Insights in production. The way I archive this is configure that with appsettings.json and appsettings.Development.json

appsettings.json:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "connectionString": "[your connection string here]",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Properties": {
      "ApplicationName": "My Worker Service App"
    }
  }
}

Substitute your Application Insights connection string. (or if this a real application, the connection string should be injected during deployment or retrieved from Key Vault or other secret store)

appsettings.Development.json:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console"
    ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "Seq", "Args": { "serverUrl": "http://localhost:5341" } }
    ],
    "Properties": {
      "ApplicationName": "My Worker Service App"
    }
  }
}

The main difference is the WriteTo section and you can configure the log level differently between production and development.

Conclusion

There you go. This is a clean setup I like to go for. You just need to inject ILogger<T> into whatever class you would like to send logs from. Simple and clean!