A Serilog sink sending log events to Grafana Loki
By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:
Glory to Ukraine! 🇺🇦
The Serilog Grafana Loki sink project is a sink (basically a writer) for the Serilog logging framework. Structured log events are written to sinks and each sink is responsible for writing it to its own backend, database, store etc. This sink delivers the data to Grafana Loki, a horizontally-scalable, highly-available, multi-tenant log aggregation system. It allows you to use Grafana for visualizing your logs.
You can find more information about what Loki is over on Grafana's website here.
Features comparison table could be found here
The list of breaking changes could be found here
The Serilog.Sinks.Grafana.Loki
NuGet package could be found here. Alternatively you can install it via one of the following commands below:
NuGet command:
Install-Package Serilog.Sinks.Grafana.Loki
.NET Core CLI:
dotnet add package Serilog.Sinks.Grafana.Loki
In the following example, the sink will send log events to Loki available on http://localhost:3100
ILogger logger = new LoggerConfiguration()
.WriteTo.GrafanaLoki(
"http://localhost:3100")
.CreateLogger();
logger.Information("The god of the day is {@God}", odin)
Used in conjunction with Serilog.Settings.Configuration the same sink can be configured in the following way:
{
"Serilog": {
"Using": [
"Serilog.Sinks.Grafana.Loki"
],
"MinimumLevel": {
"Default": "Debug"
},
"WriteTo": [
{
"Name": "GrafanaLoki",
"Args": {
"uri": "http://localhost:3100",
"labels": [
{
"key": "app",
"value": "web_app"
}
],
"propertiesAsLabels": [
"app"
]
}
}
]
}
}
Description of parameters and configuration details could be found here.
Serilog.Loki.Grafana.Loki exposes ILokiHttpClient
interface with the main operations, required for sending logs.
In order to use a custom HttpClient you can extend of default implementations:
Serilog.Sinks.Grafana.Loki.HttpClients.BaseLokiHttpClient
(implements creation of internal HttpClient
and setting credentials)Serilog.Sinks.Grafana.Loki.HttpClients.LokiHttpClient
(default client which sends logs via HTTP)Serilog.Sinks.Grafana.Loki.HttpClients.LokiGzipHttpClient
(default client which sends logs via HTTP with gzip compression)or create one implementing Serilog.Sinks.Grafana.Loki.ILokiHttpClient
.
// CustomHttpClient.cs
public class CustomHttpClient : BaseLokiHttpClient
{
public override Task<HttpResponseMessage> PostAsync(string requestUri, Stream contentStream)
{
return base.PostAsync(requestUri, contentStream);
}
}
// Usage
Log.Logger = new LoggerConfiguration()
.WriteTo.GrafanaLoki(
"http://localhost:3100",
httpClient: new CustomHttpClient()
)
.CreateLogger();
From v8 Serilog.Sinks.Grafana.Loki uses LokiJsonTextFormatter
by default, which allows to send logs to Loki as a JSON-payloads. This allows easier filtering in Loki v2, more information about how to filter can be found here
Also, you could implement your own formatter, implementing Serilog.Formatting.ITextFormatter
interface and pass it to the sink configuration.
Example configuration:
{
"Serilog": {
"Using": [
"Serilog.Sinks.Grafana.Loki"
],
"MinimumLevel": {
"Default": "Debug"
},
"WriteTo": [
{
"Name": "GrafanaLoki",
"Args": {
"uri": "http://localhost:3100",
"textFormatter": "My.Awesome.Namespace.MyTextFormatter, MyCoolAssembly"
}
}
]
}
}