C# Logging with Serilog and Seq - Structured Logging Made Easy

Share

Summary

This video introduces Serilog, an easy-to-configure logging system for .NET Core, and demonstrates how to leverage its structured logging capabilities with Seq (seq) for enhanced visualization and analysis of application events.

Highlights

Introduction to Serilog and Structured Logging
00:00:00

The video introduces Serilog as a logging system that takes advantage of .NET Core's native support for structured event logging. It highlights that Serilog is easy to configure and use, and the demonstration will cover basic logging, structured logging, and integration with Seq.

Setting up a .NET Core Web Application for Logging
00:01:29

The tutorial begins by creating a new ASP.NET Core web application in Visual Studio. This choice is made because web applications and worker services have built-in logging, making it easier to demonstrate Serilog. The project is configured for .NET Core 3.0.

Basic .NET Core Logging
00:04:16

The video demonstrates basic logging using the built-in ILogger interface in ASP.NET Core, showing how to log information messages and how they appear in the Kestrel command line output. It also illustrates how logging can provide insight into application actions like page requests and their execution times.

Adding More Log Events and an Exception Handler
00:07:42

To generate more varied log data, the video adds a `try-catch` block to handle exceptions and logs errors. A loop is introduced to log informational messages with a variable value, demonstrating how to log an exception when a specific condition is met. This section also highlights the difference between string interpolation and structured logging parameters.

Configuring Serilog in Program.cs
00:13:50

The video moves to configuring Serilog in the `Program.cs` file. It explains how to load configuration from `appsettings.json`, create a Serilog logger, and integrate it as the primary logger using `UseSerilog`. A `try-catch-finally` block is added to ensure proper initialization and closure of the logger.

Serilog Configuration in appsettings.json
00:25:00

The `appsettings.json` file is modified to configure Serilog. This includes setting minimum logging levels, overriding levels for specific namespaces (like Microsoft and System), and adding Enrichers for contextual information such as machine name, process ID, and thread ID. NuGet packages for Serilog.Enrichers are installed.

Serilog Console and File Sinks
00:32:00

The `appsettings.json` configuration is extended to include 'sinks' for writing log messages to the console and a text file. The `UseSerilogRequestLogging` middleware is added in `Startup.cs` to log all incoming HTTP requests. Running the application shows clearer, filtered logs in the console and a text file.

Structured Logging to a JSON File
00:41:25

To demonstrate true structured logging, another file sink is added to `appsettings.json` to output logs in JSON format. The video shows how to view and format this JSON log file in VS Code, highlighting how structured logs separate message templates from variable values, making them easier to query.

Introducing Seq for Visualizing Structured Logs
00:46:17

The video introduces Seq (seq), a powerful tool for visualizing and analyzing structured log data in a web-based interface. It mentions Seq's licensing model (free for individual developers, paid for teams) and emphasizes its value for gaining insights into application behavior.

Setting up Seq with Docker
00:50:35

The tutorial demonstrates how to run a Seq server using Docker, providing a simple command to deploy Seq as a container. Key aspects of the Docker command are explained, including detached mode, restart policy, naming, accepting the EULA, mapping local storage for persistence, and port forwarding.

Integrating Serilog with Seq
00:58:39

After successfully running the Seq Docker container, the video guides through extending the `appsettings.json` file to add a Serilog sink that sends log data to the running Seq server. The Serilog.Sinks.Seq NuGet package needs to be installed for this. The application is then run to populate Seq with log events.

Exploring Log Data in Seq
00:59:45

The video shows how to access the Seq dashboard in a web browser and visualize the structured log data. It demonstrates how to inspect individual log entries, view properties like `loop count value` separately from the message, and use Seq's querying capabilities to filter events (e.g., only errors). The dashboard feature provides a live overview of log levels and counts, enabling quick identification of issues.

Conclusion and Benefits of Structured Logging
01:03:35

The video concludes by reiterating the benefits of structured logging with Serilog and Seq: enhanced visibility into application behavior, easier debugging, better error identification, and the ability to track user flows. It highlights that dynamic configuration via `appsettings.json` allows for flexible log level adjustments. Structured logging is presented as a powerful addition to .NET Core development.

Recently Summarized Articles

Loading...