• Stars
    star
    188
  • Rank 205,563 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Serilog sink sending log events to Grafana Loki

Serilog.Sinks.Grafana.Loki

Made in Ukraine Build status NuGet version Latest release Documentation

Terms of use

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:

  • You condemn Russia and its military aggression against Ukraine
  • You recognize that Russia is an occupant that unlawfully invaded a sovereign state
  • You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas
  • You reject false narratives perpetuated by Russian state propaganda

Glory to Ukraine! πŸ‡ΊπŸ‡¦

Table of contents

What is this sink and Loki?

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:

  • Formats and batches log entries to Loki via HTTP (using actual API)
  • Global and contextual labels support
  • Flexible Loki labels configuration possibilities
  • Collision avoiding mechanism for labels
  • Integration with Serilog.Settings.Configuration
  • Customizable HTTP clients
  • HTTP client with gzip compression
  • Using fast System.Text.Json library for serialization
  • Possibility of sending json logs to Loki
  • No dependencies on another sinks

Comparison

Features comparison table could be found here

Breaking changes

The list of breaking changes could be found here

Quickstart

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.

Custom HTTP Client

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();

Sending json content to Loki

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"
        }
      }
    ]
  }
}

Inspiration and Credits

More Repositories

1

serilog-sinks-elasticsearch

A Serilog sink that writes events to Elasticsearch
C#
434
star
2

serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
C#
219
star
3

serilog-ui

Simple Serilog log viewer UI for several sinks.
C#
207
star
4

Serilog.Enrichers.Sensitive

A Serilog LogEvent enricher that masks sensitive data
C#
112
star
5

serilog-sinks-richtextbox

A Serilog sink that writes log events to a WPF RichTextBox control with colors and theme support
C#
97
star
6

serilog-enrichers-clientinfo

Enrich logs with client IP, correlation id and HTTP request headers.
C#
90
star
7

serilog-sinks-graylog

Serilog sink for graylog
C#
81
star
8

serilog-sinks-notepad

A Serilog sink that writes log events to Notepad as text or JSON
C#
61
star
9

Serilog.Sinks.Postgresql.Alternative

Serilog.Sinks.Postgresql.Alternative is a library to save logging information from https://github.com/serilog/serilog to https://www.postgresql.org/.
C#
59
star
10

SerilogSinksInMemory

In-memory sink for Serilog to use for testing
C#
53
star
11

serilog-sinks-splunk

A Serilog sink that writes to Splunk
C#
45
star
12

serilog-sinks-slack

A simple (yet customizable) Slack logging sink for Serilog
C#
41
star
13

serilog-sinks-azuretablestorage

A Serilog sink that writes events to Azure Table Storage
C#
40
star
14

serilog-sinks-sentry

A Sentry sink for Serilog
C#
36
star
15

Serilog.Sinks.Telegram.Alternative

Serilog.Sinks.Telegram.Alternative is a library to save logging information from Serilog to Telegram.
C#
35
star
16

Serilog.Sinks.MicrosoftTeams.Alternative

Serilog.Sinks.MicrosoftTeams.Alternative is a library to save logging information from Serilog to Microsoft Teams.
C#
30
star
17

serilog-sinks-slackclient

Slack Sink for Serilog
C#
27
star
18

serilog-diagnostics-tracelistener

A System.Diagnostics.TraceListener that writes trace data into Serilog
C#
25
star
19

serilog-enrichers-globallogcontext

A Serilog Enricher for adding properties to all log events in your app
C#
25
star
20

Serilog.Sinks.AmazonS3

Serilog.Sinks.AmazonS3 is a library to save logging information from Serilog to Amazon S3. The idea there was to upload log files to Amazon S3 to later evaluate them with Amazon EMR services.
C#
21
star
21

serilog-sinks-teams

A Serilog event sink that writes to Microsoft Teams
C#
18
star
22

Serilog-Sinks-Discord

Serilog discord sink
C#
17
star
23

Serilog.Sinks.Network

A serilog network sink. Designed with logstash and the Elastic stack in mind
C#
16
star
24

home

This is the hub for all the projects that are part of the Serilog Contrib Organization
16
star
25

Serilog.Sinks.Logz.Io

C#
16
star
26

serilog-sinks-exceldnalogdisplay

A Serilog sink that writes events to Excel-DNA LogDisplay
C#
13
star
27

serilog-sinks-azureeventhub

A Serilog sink that writes events to Azure EventHubs
C#
13
star
28

serilog-sinks-rollbar

A Serilog sink which writes events to Rollbar
C#
10
star
29

Serilog.Logfmt

Simple Serilog formatter to output Logfmt
C#
9
star
30

serilog-enrichers-exceldna

A Serilog Enricher with properties from Excel-DNA add-ins
C#
6
star
31

serilog-enrichers-memory

An enricher for Serilog which outputs memory usage
C#
6
star
32

serilog-sinks-apachekafka

A sink for Serilog that writes events to Kafka
C#
6
star
33

serilog-sinks-msbuild

An MSBuild sink for Serilog.
C#
5
star
34

serilog-settings-xml

Xml file based configuration for Serilog (https://serilog.net)
C#
5
star
35

serilog-sinks-OnePageSink

Sink that displays real-time messages from Serilog in a separate web page. Uses SignalR as a transport. Stores nothing, uses no memory. If you refresh your page you will lose your messages
C#
4
star
36

serilog-formatting-log4net

Format Serilog events in log4net or log4j compatible XML format
C#
4
star
37

Serilog.Sinks.DelegatingText

A Serilog sink to emit formatted log events to a delegate.
C#
3
star
38

brand

Guide and reference to designers, writers, and developers to create consistent, on-brand content for Serilog
2
star
39

serilog-sinks-youtrack

A Serilog sink that writes events to YouTrack
C#
2
star
40

Serilog.Enrichers.ActivityTags

C#
1
star
41

Serilog.Sinks.Logcat

Android logcat sink extension for the Serilog logger
C#
1
star
42

Serilog.Enrichers.ApplicationName

PowerShell
1
star
43

serilog-sinks-amqp-batching

C#
1
star
44

serilog-settings-xml2

A Serilog settings provider that reads from XML sources with zero dependencies and full configuration support
C#
1
star