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 to 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 ASP.NET Core Web API project using Serilog with sink SEQ.

Key features of SEQ

Here are some key features of SEQ.

Centralized Log Management: SEQ collects and centralizes logs from multiple applications.

Structured Logging: Logs are stored as structured data (e.g. JSON). This enables better querying and filtering compared to plain text logs.

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.

Dashboards and Alerts: Visualize log trends with dashboards. It sets up alerts based on specific log events or thresholds.

Integrations: SEQ integrates with tools such as Serilog, NLog, Microsoft.Extensions.Logging, and more. It supports integrations with other platforms like Slack, Microsoft Teams, and PagerDuty for notifications.

Real-Time Insights: It provides real-time insights into the behavior and performance of your applications.

How to use SEQ in Aplication

Install SEQ in your machine

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

Implementing SEQ in the application

Step 1 -Create a new or use existing project

We can use existing project or create new project where we want to implement logging using SEQ. In this article we will create a new project for demonstration on how to implement from the beginning.

Let’s create 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.

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

Select project framework: .NET 9 as depicted below.

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

  • Serilog
  • Serilog.Sinks.Seq
  • Serilog.AspNetCore
  • Serilog.Extensions.Hosting

Step 3 – After the installation of necessary packages, we will need to configure for 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 the any C# class. For an example if we want to log the GetWeatherForecast API call with 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, exception will be captured in the SEQ.

Step 7Run application and Check the Log

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

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

In the above screen you we can see that there is different tabs for event logs such as Errors, warnings, exceptions which is 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 found this information valuable and insightful.

Leave a Reply

Your email address will not be published. Required fields are marked *