Skip to content

Instantly share code, notes, and snippets.

@sarmis
Last active February 25, 2024 18:07
Show Gist options
  • Save sarmis/1cb6a3cfbebae0d5c6b585e47ac85826 to your computer and use it in GitHub Desktop.
Save sarmis/1cb6a3cfbebae0d5c6b585e47ac85826 to your computer and use it in GitHub Desktop.
Grafana & Loki
**/bin/
**/obj/
docker run --name gdt-grafana -d -p 3000:3000 grafana/grafana
docker run --name gdt-loki -d -p 3100:3100 grafana/loki
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
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();
}
}
dotnet new console --name GDT.Logging.ConsoleApp
cd GDT.Logging.ConsoleApp
dotnet add package Serilog
dotnet add package Serilog.Settings.Configuration
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.Grafana.Loki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment