Last active
February 25, 2024 18:07
-
-
Save sarmis/1cb6a3cfbebae0d5c6b585e47ac85826 to your computer and use it in GitHub Desktop.
Grafana & Loki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**/bin/ | |
**/obj/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run --name gdt-grafana -d -p 3000:3000 grafana/grafana | |
docker run --name gdt-loki -d -p 3100:3100 grafana/loki |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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