C# - Logging with Grafana & Loki

Disclaimer

This post aims to be a quick guide on how to setup Grafana, Loki and your C# application to be able to send logs into loki and inspect them via Grafana. It does not cover best practices, advanced scenarios or logs/tracing/metrics linking which will be covered in a future post.

Introduction

In the world of software development, logging is an indispensable tool for monitoring applications, troubleshooting issues, and improving performance. However, managing logs can be a daunting task due to the volume of data generated and the need for efficient analysis tools.

Enter Loki and Grafana, a powerful combination for managing and visualizing logs. Loki, developed by Grafana Labs, is a log aggregation system inspired by Prometheus. Paired with Grafana, an open-source platform for monitoring and observability, Loki enables developers to visualize log data through dashboards, making it easier to understand patterns, track down errors, and gain insights into their applications.

This blog post aims to guide you through the process of setting up the infrastructure and integrating logging in your C# applications using Loki, followed by visualizing and analyzing those logs with Grafana.

Infrastructure Setup

Grafana and Loki are required for this tutorial, the most common option for setting up infrastructure is containers.

Setting up with Docker is straight forward:

1
2
\> docker run --name gdt-grafana -d -p 3000:3000 grafana/grafana
\> docker run --name gdt-loki -d -p 3100:3100 grafana/loki 

Setting up with Podman requires to create a custom network first because Podman does not support dns resolving on the default network:

1
2
3
4
\> podman network create gdt-net

\> podman run --network gdt-net --name gdt-grafana -d -p 3000:3000 grafana/grafana
\> podman run --network gdt-net --name gdt-loki -d -p 3100:3100 grafana/loki 

Validation

Infrastructure Configuration

The next step is to connect grafana with loki:

  1. Login to (Grafana)[http://localhost:3000],
  2. Open the main menu from the 3-bars icon on top left
  3. Navigate to “Connections”
  4. Enter “Loki” on the search bar and Select the “Loki” options from the results
  5. Click on the “Add new data source” button on top right
  6. Type http://gdt-loki:3100 on the “url” field
  7. Click the “Save & Test” button on the bottom of the page

Application Setup

  1. Create a new dotnet application
1
\> dotnet new console --name GDT.Logging.ConsoleApp
  1. Install the following packages (run inside te application folder)
1
2
3
4
\> dotnet add package Serilog
\> dotnet add package Serilog.Settings.Configuration
\> dotnet add package Serilog.Sinks.Console
\> dotnet add package Serilog.Sinks.Grafana.Loki
  1. Paste the following on program.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Runtime.CompilerServices;
using Serilog;
using Serilog.Context;
using Serilog.Sinks.Grafana.Loki;

class Program
{   
    const string facility = "gdt-tracing-consoleapp";

    static async Task Main(string[] args)
    {
        // Create a "facility" label to group all our logs under
        var facilityLabel = new LokiLabel() { 
            Key = "facility", 
            Value = facility };

        // Setup Logger with Console & Loki sinks (outputs)
        Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .WriteTo.GrafanaLoki(
                "http://localhost:3100", 
                new List<LokiLabel>() { facilityLabel })
            .CreateLogger();            

        // Add the "action" property to all logs inside the using scope
        using (LogContext.PushProperty("action", "gdt-custom-action"))
        {
            Log.Logger.Information("Starting custom action");
            await Task.Delay(TimeSpan.FromMilliseconds(200));
            Log.Logger.Information("Custom action completed");
        }

        // Flush the logs to ensure being send to loki
        Log.CloseAndFlush();
    }
}
  1. Run the application
1
\> dotnet run

Log Visualization / Inspection

In order to view the logs we need to open Grafana and configure the visualization.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy