-
Notifications
You must be signed in to change notification settings - Fork 30
Using LokiJsonTextFormatter and Grafana Loki json parser
You need Loki 2.0 or higher and Serilog.Sinks.Grafana.Loki v5.1.2 or higher
From Loki 2.0 you could extract labels from log lines at query time. This will help us to keep a cardinality low and query through the logs.
From Serilog.Sinks.Grafana.Loki v8.0.0 LokiJsonTextFormatter
is used as a default formatter in the sink
Serilog.Sinks.Grafana.Loki.SampleWebApp
is used in the next example.
For example, we have a standard web application with a next configuration:
{
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.Grafana.Loki"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [
"WithThreadId"
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:dd-MM-yyyy HH:mm:ss} [{Level:u3}] [{ThreadId}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "GrafanaLoki",
"Args": {
"uri": "http://localhost:3100",
"labels": [
{
"key": "app",
"value": "web_app"
}
],
"propertiesAsLabels": [
"app"
]
}
}
]
}
}
Simple logging method is
[HttpGet("info")]
public IActionResult GetInfo()
{
var odin = new {Id = 1, Name = "Odin"};
_logger.LogInformation("God of the day {@God}", odin);
return Ok(odin);
}
In this case, using next filter
{app="web_app"}
we could see
In this case you see metadata as a detected fields. This is cool, but not useful for querying.
So the next step is to add a json
parser, which will turn our detected fields into query time labels.
{app="web_app"} | json
Our message will be destructured into:
And the next step will be adding the filter:
{app="web_app"} | json | ConnectionId="0HMD1AQG310G0"
And your result will be filtered by criteria.