Serilog.Sinks.GrafanaLoki 1.0.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package Serilog.Sinks.GrafanaLoki --version 1.0.0
NuGet\Install-Package Serilog.Sinks.GrafanaLoki -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.GrafanaLoki" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.GrafanaLoki --version 1.0.0
#r "nuget: Serilog.Sinks.GrafanaLoki, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Sinks.GrafanaLoki as a Cake Addin
#addin nuget:?package=Serilog.Sinks.GrafanaLoki&version=1.0.0

// Install Serilog.Sinks.GrafanaLoki as a Cake Tool
#tool nuget:?package=Serilog.Sinks.GrafanaLoki&version=1.0.0


A Serilog Sink for Grafana's Loki Log Aggregator.

What is Loki?

Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be very cost effective and easy to operate, as it does not index the contents of the logs, but rather a set of labels for each log stream.

You can find more information about what Loki is over on Grafana's website here.


  • Uses the new Loki HTTP API
  • Serilog.Settings.Configuration integration (configure sink via configuration file, JSON sample provided in Example project)
  • Global and contextual labels support
  • Logs are send to Loki via HTTP using Serilog.Sinks.Http package (batch support included)
  • Customizable HTTP client


The Serilog.Sinks.GrafanaLoki NuGet package can be found here. Alternatively you can install it via one of the following commands below:

NuGet command:

Install-Package Serilog.Sinks.GrafanaLoki

.NET Core CLI:

dotnet add package Serilog.Sinks.GrafanaLoki

Basic Example:

var credentials = new GrafanaLokiCredentials()
    User = "<username>",
    Password = "<password>"

Log.Logger = new LoggerConfiguration()
    .Enrich.WithProperty("ALabel", "ALabelValue")
        new List<GrafanaLokiLabel>() { new GrafanaLokiLabel() { Key = "app", Value = "Serilog.Sinks.GrafanaLoki.Example" } }, // Global labels

Log.Information("Logs are now sent to Loki at address {@Url}.", "http://localhost:3100");


Adding contextual (local) labels

If you need to add contextual labels from a particular class or method, you can achieve this with the following code:

using (LogContext.PushProperty("ALabel", "ALabelValue"))
    log.Information("Info with ALabel");
    log.Warning("Warning with ALabel");

Custom HTTP Client

Serilog.Loki.GrafanaLoki is built on top of the popular Serilog.Sinks.Http library to post log entries to Loki. In order to use a custom HttpClient you can extend the default HttpClient (Serilog.Sinks.GrafanaLoki.GrafanaLokiHttpClient), or create one from scratch by implementing Serilog.Sinks.GrafanaLoki.IGrafanaLokiHttpClient (which extends Serilog.Sinks.Http.IHttpClient).

// CustomHttpClient.cs

public class CustomHttpClient : GrafanaLokiHttpClient
    public override async Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
        var req = content.ReadAsStringAsync().Result;
        var response = await base.PostAsync(requestUri, content);
        var body = response.Content.ReadAsStringAsync().Result;
        return response;
// Usage

var credentials = new GrafanaLokiCredentials()
    User = "<username>",
    Password = "<password>"

Log.Logger = new LoggerConfiguration()
    .Enrich.WithProperty("ALabel", "ALabelValue")
        url: "http://localhost:3100",
        credentials: credentials,
        httpClient: new CustomHttpClient()

Using application settings configuration (appsettings.json)

In order to configure this sink using Microsoft.Extensions.Configuration, for example with ASP.NET Core or .NET Core, the package has as dependency the Serilog.Settings.Configuration package. This example is for the JSON configuration file, but it should work fine with any configuration source (.ini, XML etc.) by making the appropriate format changes.

Instead of configuring the sink directly in code, you can make all the configurations in the configuration file and then just call ReadFrom.Configuration() method:

var configuration = new ConfigurationBuilder()

var logger = new LoggerConfiguration()

appsettings.json configuration sample:

  "Serilog": {
    "WriteTo": [
        "Name": "GrafanaLoki",
        "Args": {
          "Url": "http://localhost:3100",
          "Credentials": {
            "User": "",
            "Password": ""
          "restrictedToMinimumLevel": "Debug",
          "outputTemplate": "{Timestamp:HH:mm:ss} | {Level:u3} | {Message:lj} | {Exception:1}",
          "Labels": [

              "Key": "project",
              "Value": "Serilog.Sinks.GrafanaLoki"
              "Key": "app",
              "value": "Serilog.Sinks.GrafanaLoki.Example"
          "batchPostingLimit": 1000,
          "queueLimit": null,
          "period": null

Excepting the Url, all configuration options are optional.

Inspiration and Credits

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 6,974 7/15/2022
1.1.0 243 7/13/2022
1.0.2 5,517 7/9/2020
1.0.1 389 7/9/2020
1.0.0 474 4/23/2020
1.0.0-beta.1 199 4/22/2020