VOOZH about

URL: https://www.nuget.org/packages/Serilog.Sinks.GrafanaLoki/

⇱ NuGet Gallery | Serilog.Sinks.GrafanaLoki 1.1.2




👁 Image
Serilog.Sinks.GrafanaLoki 1.1.2

dotnet add package Serilog.Sinks.GrafanaLoki --version 1.1.2
 
 
NuGet\Install-Package Serilog.Sinks.GrafanaLoki -Version 1.1.2
 
 
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.1.2" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Sinks.GrafanaLoki" Version="1.1.2" />
 
Directory.Packages.props
<PackageReference Include="Serilog.Sinks.GrafanaLoki" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Serilog.Sinks.GrafanaLoki --version 1.1.2
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Serilog.Sinks.GrafanaLoki, 1.1.2"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Serilog.Sinks.GrafanaLoki@1.1.2
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Serilog.Sinks.GrafanaLoki&version=1.1.2
 
Install as a Cake Addin
#tool nuget:?package=Serilog.Sinks.GrafanaLoki&version=1.1.2
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Serilog.Sinks.GrafanaLoki

A Serilog Sink for Grafana's Loki Log Aggregator.

👁 .NET build

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.

Features:

  • Timestamps precision at 100ns (lower risk of collision between log entries)
  • 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
  • Log entries are grouped in Streams by log level and other contextual labels
  • Logs are send to Loki in batches via HTTP using internal client
  • Customizable HTTP client

Installation

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()
 .MinimumLevel.Verbose()
 .Enrich.FromLogContext()
 .Enrich.WithProperty("ALabel", "ALabelValue")
 .WriteTo.GrafanaLoki(
 "http://localhost:3100",
 credentials,
 new Dictionary<string, string>() { { "app", "Serilog.Sinks.GrafanaLoki.Sample" } }, // Global labels
 Events.LogEventLevel.Debug
 )
 .CreateLogger();

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

Log.CloseAndFlush();

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 uses by default the internal HTTP Client, but you can customize it by implementing the Serilog.Sinks.GrafanaLoki.Common.IHttpClient interface or by extending the Serilog.Sinks.GrafanaLoki.GrafanaLokiHttpClient class.

// CustomHttpClient.cs

public class CustomHttpClient : GrafanaLokiHttpClient
{
 public override async Task<HttpResponseMessage> PostAsync(string requestUri, Stream contentStream)
 {
 using var content = new StreamContent(contentStream);
 content.Headers.Add("Content-Type", "application/json");
 var response = await HttpClient
 .PostAsync(requestUri, content)
 .ConfigureAwait(false);
 return response;
 }
}
// Usage

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

Log.Logger = new LoggerConfiguration()
 .MinimumLevel.Verbose()
 .Enrich.FromLogContext()
 .Enrich.WithProperty("ALabel", "ALabelValue")
 .WriteTo.GrafanaLoki(
 url: "http://localhost:3100",
 credentials: credentials,
 httpClient: new CustomHttpClient()
 )
 .CreateLogger();

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()
 .AddJsonFile("appsettings.json")
 .Build();

var logger = new LoggerConfiguration()
 .ReadFrom.Configuration(configuration)
 .CreateLogger();

appsettings.json configuration sample:

{
 "Serilog": {
 "WriteTo": [
 {
 "Name": "GrafanaLoki",
 "Args": {
 "Url": "http://localhost:3100",
 "Credentials": {
 "User": "<username>",
 "Password": "<password>"
 },
 "Labels": {
 "project": "Serilog.Sinks.GrafanaLoki",
 "app": "Serilog.Sinks.GrafanaLoki.Sample"
 },
 "restrictedToMinimumLevel": "Debug",
 "outputTemplate": "{Timestamp:HH:mm:ss} [{Level:u3}] | {Message:lj} | {Exception:1}",
 "propertiesStringDelimiter": null,
 "logEventsInBatchLimit": 1000,
 "queueLimitBytes": null,
 "logEventLimitBytes": null,
 "period": null,
 "httpRequestTimeout": 3000,
 "debugMode": true
 }
 }
 ]
 }
}

Excepting the Url, all configuration items are optional.

Inspiration and Credits

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

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Serilog.Sinks.GrafanaLoki:

Package Downloads
SharedX.Abstractions

Abstractions for caching, messaging, persistence, and tracing in SharedX microservices

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.2 55,431 9/30/2023
1.1.1 47,285 7/15/2022
1.1.0 1,717 7/13/2022
1.0.2 11,257 7/9/2020
1.0.1 1,830 7/9/2020
1.0.0 2,001 4/23/2020
1.0.0-beta.1 464 4/22/2020