Summary
Highlights
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.