ggmueller / nlog.loki

NLog Loki Target

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NLog Loki Target

CI NuGet codecov

This is an NLog target that sends messages to Loki using Loki's HTTP Push API.

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.

Installation

The NLog.Loki NuGet package can be found here. You can install it via one of the following commands below:

NuGet command:

Install-Package CorentinAltepe.NLog.Loki

.NET Core CLI command:

dotnet add package CorentinAltepe.NLog.Loki

Usage

Under .NET Core, remember to register NLog.Loki as an extension assembly with NLog. You can now add a Loki target to your configuration file:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <extensions>
    <add assembly="NLog.Loki" />
  </extensions>

  <!-- Loki target is async, so there is no need to wrap it in an async target wrapper. -->
  <targets>
    <target 
      name="loki" 
      xsi:type="loki"
      batchSize="200"
      taskDelayMilliseconds="500"
      queueLimit="10000"
      endpoint="http://localhost:3100"
      username="myusername"
      password="secret"
      layout="${level}|${message}${onexception:|${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}|source=${logger}">
      <!-- Loki requires at least one label associated with the log stream. 
      Make sure you specify at least one label here. -->
      <label name="level" layout="${level:lowercase=true}" />
      <label name="server" layout="${hostname:lowercase=true}" />
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="loki" />
  </rules>

</nlog>

The @endpoint attribute is a Layout that must ultimately resolve to a fully-qualified absolute URL of the Loki Server when running in a Single Proccess Mode or of the Loki Distributor when running in Microservices Mode. When an invalid URI is encountered, all log messages are silently discarded.

username and password are optional fields, used for basic authentication with Loki.

label elements can be used to enrich messages with additional labels. label/@layout support usual NLog layout renderers.

About

NLog Loki Target

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:C# 100.0%