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:
|
|
Setting up with Podman requires to create a custom network first because Podman does not support dns resolving on the default network:
|
|
Validation
- Grafana: Open http://localhost:3000 (credentials: admin/admin)
- Loki: open http://localhost:3100/ready - (a few refreshs may be required until the response is
ready
)
Infrastructure Configuration
The next step is to connect grafana with loki:
- Login to (Grafana)[http://localhost:3000],
- Open the main menu from the 3-bars icon on top left
- Navigate to “Connections”
- Enter “Loki” on the search bar and Select the “Loki” options from the results
- Click on the “Add new data source” button on top right
- Type
http://gdt-loki:3100
on the “url” field - Click the “Save & Test” button on the bottom of the page
Application Setup
- Create a new dotnet application
|
|
- Install the following packages (run inside te application folder)
|
|
- Paste the following on
program.cs
|
|
- Run the application
|
|
Log Visualization / Inspection
In order to view the logs we need to open Grafana and configure the visualization.