Serilog.Sinks.GrafanaLoki
1.0.0
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
<PackageReference Include="Serilog.Sinks.GrafanaLoki" Version="1.0.0" />
paket add Serilog.Sinks.GrafanaLoki --version 1.0.0
#r "nuget: Serilog.Sinks.GrafanaLoki, 1.0.0"
// 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
Serilog.Sinks.GrafanaLoki
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.
Features:
- 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
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 List<GrafanaLokiLabel>() { new GrafanaLokiLabel() { Key = "app", Value = "Serilog.Sinks.GrafanaLoki.Example" } }, // 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 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()
.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": "",
"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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration.Json (>= 3.1.3)
- Serilog (>= 2.9.0)
- Serilog.Settings.Configuration (>= 3.1.0)
- Serilog.Sinks.Http (>= 5.2.1)
- System.Configuration.ConfigurationManager (>= 4.7.0)
- System.Net.Http (>= 4.3.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.