Enhancing Application Insights with Serilog and SEQ

Introduction

Serilog is a diagnostic logging library tailored for .NET applications. It offers a straightforward, flexible, and robust solution for logging application events, errors, and other critical information. One of its standout features is its support for structured logging, which allows developers to log detailed, context-rich data instead of simple plain-text messages. To know more about it you can visit the previous article here.

SEQ is a structured log server that simplifies the management, visualization, and analysis of application logs. It is especially well suited for .NET applications, but it can also be used with any other platform that supports structured logging. In this article, we will learn application logging in the ASP.NET Core Web API project using Serilog with sink SEQ.

Key features of SEQ

Here are some key features of SEQ.

  1. Centralized Log Management: SEQ collects and centralizes logs from multiple applications.
  2. Structured Logging: Logs are stored as structured data (e.g. JSON). This enables better querying and filtering compared to plain text logs.
  3. Powerful query language: SEQ provides a SQL-like query language that allows you to filter and search logs based on properties, timestamps, or the content of messages.
  4. Dashboards and Alerts: Visualize log trends with dashboards. It sets up alerts based on specific log events or thresholds.
  5. Integrations: SEQ integrates with tools such as Serilog, NLog, and Microsoft.Extensions.Logging, and more. It supports notifications integrations with other platforms like Slack, Microsoft Teams, and PagerDuty.
  6. Real-Time Insights: It provides real-time insights into the behavior and performance of your applications.

How to use SEQ in the application?

Install SEQ on your machine

Firstly, we have to install SEQ on our machine. Download and install SEQ from here.

Implementing SEQ in the application

Step 1. Create a new or use an existing project.

We can use existing projects or create new projects where we want to implement logging using SEQ. In this article, we will create a new project to demonstrate how to implement it from the beginning.

Let’s create a new WebAPI project.

Create a new ASP.NET Core Web API application in .NET9 framework or use your existing project where you need to use Serilog logging with SEQ.

Web API application

Web API

Give the project name, select the location of the project, and Click on Next.

Project Name

Select project framework .NET 9 as depicted below.

Select Project

Step 2. Install the logging library Serilog and Serilog sink in your project from the NuGet package manager.

  1. Serilog
  2. Serilog.Sinks.Seq
  3. Serilog.AspNetCore
  4. Serilog.Extensions.Hosting

Step 3. After the installation of the necessary packages, we will need to configure Serilog in the appsetting.json file.

{
  "Serilog": {
    "Using": ["Serilog.Sinks.Seq"],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Seq",
        "Args": {
          "serverUrl": "http://localhost:5341",
          "apiKey": "your-seq-api-key"
        }
      },
      {
        "Name": "Console"
      }
    ],
    "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
    "Properties": {
      "Application": "MyAppName"
    }
  },
  "AllowedHosts": "*"
}

Step 4. In the Program.cs we will add the Serilog configuration.

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .CreateLogger();

builder.Host.UseSerilog();

Step 5. Then, above the App.Run() write Serilog middleware.

app.UseSerilogRequestLogging();

Step 6. Then we can now log any custom message in any C# class. For example, if we want to log the GetWeatherForecast API call with a custom message, then below is an example.

using Microsoft.AspNetCore.Mvc;

namespace DemoLoggerWithSerilogAndSEQ.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            _logger.LogInformation("Get weather forecast called!"); // Logging with customized message
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

Note. By default, any information, warning, or exception will be captured in the SEQ.

Step 7. Run the application and Check the Log.

Now, we can run the application and check the logs in application insights.

Sample Logging of this demo application in SEQ is illustrated below.

 SEQ

In the above screen, we can see that there are different tabs for event logs, such as Errors, warnings, and exceptions, which makes it easy to filter and investigate the issue. As mentioned earlier you can see in the above image Dashboard, Alerts, Data, and so on in the SEQ.

Conclusion

Integrating Serilog into your application enables developers to enhance debugging, monitoring, and system maintenance by producing detailed and actionable log data. With its structured logging capabilities, Serilog simplifies log analysis and visualization, making it a vital tool for modern software solutions. Leveraging Serilog can significantly boost your application’s traceability, reliability, and overall efficiency. In this guide, we’ve explored how to implement logging with Serilog and SEQ. I hope you have found this information valuable and insightful.

Reference: Application Logging in .NET 9