Skip to content

Using LokiJsonTextFormatter and Grafana Loki json parser

Mykhailo Shevchuk edited this page Jul 19, 2022 · 3 revisions

Prerequisites

You need Loki 2.0 or higher and Serilog.Sinks.Grafana.Loki v5.1.2 or higher

What is the feature?

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

How it works?

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 1

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: 2

And the next step will be adding the filter:

{app="web_app"} | json | ConnectionId="0HMD1AQG310G0"

And your result will be filtered by criteria.