Integrating Serilog with Loki for Efficient Logging in .NET Applications

Integrating Serilog with Loki for Efficient Logging in .NET Applications

Introduction

Effective logging is vital for monitoring, troubleshooting, and understanding application behaviour. For .NET developers, Serilog offers a great logging framework, and when combined with Loki, Grafana’s log aggregation system, it provides a powerful tool for managing logs.

This blog post will guide you through the steps to send Serilog output to Loki, enhancing your application’s logging capabilities.

Introduction to Serilog and Loki

Serilog is a popular .NET logging library known for its structured logging approach. It allows logging complex data types and supports various output targets (sinks). I have written several blog posts about it – just use the search option to find them.

Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus. It’s designed to be very cost-effective and easy to operate, focusing on logs indexing and leaving the storage to more efficient and cost-effective solutions.

Integrating Serilog with Loki enables you to funnel your logs into a centralised system where they can be queried, visualised, and analyzed with ease.

Prerequisites

  • Basic knowledge of .NET and C#.
  • An ASP.NET Core application where Serilog is used for logging.
  • A Loki server setup (either self-hosted or through Grafana Cloud).

Setting Up Serilog

Ensure that Serilog is set up in your ASP.NET Core application. If not, you can install it via NuGet and set it up in your Program.cs or Startup.cs file.

Installing the Loki Sink

To send logs from Serilog to Loki, you need to install the appropriate sink. You can find a Loki sink for Serilog like Serilog.Sinks.Loki. Install it via NuGet Package Manager:

Install-Package Serilog.Sinks.Loki

Configuring Serilog to Use Loki Sink

Once the Loki sink is installed, configure Serilog to use this sink in your application’s startup configuration. Here’s an example:

var log = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.LokiHttp(credentials: new NoAuthCredentials("http://localhost:3100"))
    .CreateLogger();

Replace "http://localhost:3100" with the URL of your Loki instance.

Structuring Your Logs

One of Serilog’s strengths is structured logging. When sending logs to Loki, take advantage of this feature to enrich your logs with useful context. For example:

log.Information("User {UserId} logged in at {LoggedInAt}", user.Id, DateTime.UtcNow);

Querying Logs in Loki

Once your logs are being sent to Loki, you can query them using Grafana. Loki uses a query language similar to Prometheus and allows you to filter and search logs based on time, log levels, and other structured data you’ve logged.

Visualising Logs

Grafana excels in visualising data, and this extends to logs stored in Loki. You can create dashboards in Grafana to visualise your log data, set up alerts, and gain insights into your application’s behaviour and performance.

Best Practices for Serilog and Loki

  1. Structured Logging: Make full use of Serilog’s structured logging capabilities to enrich your logs.
  2. Log Levels: Use appropriate log levels to differentiate between informational, debug, and error logs.
  3. Sensitive Data: Be cautious about logging sensitive information.
  4. Monitoring and Alerting: Utilise Grafana’s alerting features to monitor critical and unusual activities in your logs.

Conclusion

Integrating Serilog with Loki offers a powerful combination for logging in .NET applications, providing an efficient way to aggregate, query, and visualise logs.

By following the steps outlined above, developers can set up a robust logging system that enhances the observability and diagnostics capabilities of their applications.

Remember, effective logging is not just about recording data; it’s about making that data actionable and insightful.

Serilog can be downloaded from here: – https://serilog.net

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.