![]() |
VOOZH | about |
dotnet add package NLog.Targets.Loki --version 2.5.0
NuGet\Install-Package NLog.Targets.Loki -Version 2.5.0
<PackageReference Include="NLog.Targets.Loki" Version="2.5.0" />
<PackageVersion Include="NLog.Targets.Loki" Version="2.5.0" />Directory.Packages.props
<PackageReference Include="NLog.Targets.Loki" />Project file
paket add NLog.Targets.Loki --version 2.5.0
#r "nuget: NLog.Targets.Loki, 2.5.0"
#:package NLog.Targets.Loki@2.5.0
#addin nuget:?package=NLog.Targets.Loki&version=2.5.0Install as a Cake Addin
#tool nuget:?package=NLog.Targets.Loki&version=2.5.0Install as a Cake Tool
This is an NLog target that sends messages to Grafana Loki using Grafana Loki's HTTP Push API. Available on .NET Standard 2.0 (.NET Core 2.0, .NET Framework 4.6.1 and higher). For a gRPC client implementation, please use this target instead.
Grafana 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.
The NLog.Loki NuGet package can be found here. You can install it via one of the following commands below:
NuGet command:
Install-Package NLog.Targets.Loki
.NET Core CLI command:
dotnet add package NLog.Targets.Loki
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>
<targets>
<target
name="loki"
xsi:type="loki"
batchSize="200"
taskDelayMilliseconds="500"
endpoint="http://localhost:3100"
username="myusername"
password="secret"
orderWrites="true"
compressionLevel="noCompression"
layout="${level}|${message}${onexception:|${exception:format=type,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}|source=${logger}"
eventPropertiesAsLabels="false"
tenant="tenantid"
proxyUrl="http://proxy:8888"
proxyUser="username"
proxyPassword="secret">
<layout xsi:type="JsonLayout" includeEventProperties="true">
<attribute name="level" layout="${level}" />
<attribute name="source" layout="${logger}" />
<attribute name="message" layout="${message}" />
<attribute name="exception" encode="false">
<layout xsi:type="JsonLayout">
<attribute name="type" layout="${exception:format=type}" />
<attribute name="message" layout="${exception:format=message}" />
<attribute name="stacktrace" layout="${exception:format=tostring}" />
</layout>
</attribute>
</layout>
<label name="app" layout="my-app-name" />
<label name="server" layout="${hostname:lowercase=true}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="loki" />
</rules>
</nlog>
endpoint must 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.
username and password are optional fields, used for basic authentication with Grafana Loki.
orderWrites - Orders the logs by timestamp before sending them to Grafana loki when logs are batched in a single HTTP call. This is required if you use Loki v2.3 or lower. But it is not required if you use Grafana Loki v2.4 or higher (see out-of-order writes). You are strongly advised to set this value to false when using Grafana Loki v2.4+ since it reduces allocations by about 20% by the serializer (default false).
compressionLevel - Gzip compression level applied if any when sending messages to Grafana Loki (default optimal). Possible values:
noCompression: no compression applied, HTTP header will not specify a Content-Encoding with gzip value.fastest: the compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.optimal: the compression operation should be optimally compressed, even if the operation takes a longer time to complete.smallestSize: supported by .NET 6 or greater only. The compression operation should create output as small as possible, even if the operation takes a longer time to complete.label elements can be used to enrich messages with additional labels. label/@layout support usual NLog layout renderers.
layout - While it is possible to define a simple layout structure in the attributes of the target configuration,
prefer using a JsonLayout to structure your logs. This will allow better parsing in Grafana Loki.
eventPropertiesAsLabels: creates one Grafana Loki's label per event property. Beware, this goes against Grafana Loki's best practices since Too many label value combinations leads to too many streams. In order to structure your logs, you are advised to keep away from this feature and to use the JsonLayout provided in the example (default false).
tenant - Tenant ID for a multi-tenant configuration. See Grafana Loki's Multi-tenant documentation. If specified, applied as X-Scope-OrgID HTTP Header on requests sent to Loki.
proxyUrl - URL to the proxy server to use. Must include protocol (http|https) and port. If not specified, then no proxy is used (default null).
proxyUser - username to use for authentication with proxy.
proxyPassword - password to use for authentication with proxy.
NLog.Loki is an async target. You should not wrap it in an AsyncWrapper target. The following configuration options are supported. Make sure to adjust them to the expected throughput and criticality of your application's logs, especially the batch size, retry count and task delay.
taskTimeoutSeconds - How many seconds a Task is allowed to run before it is cancelled (default 150 secs).
retryDelayMilliseconds - How many milliseconds to wait before next retry (default 500ms, and will be doubled on each retry).
retryCount - How many attempts to retry the same Task, before it is aborted (default 0, meaning no retry).
batchSize - Gets or sets the number of log events that should be processed in a batch by the lazy writer thread (default 1).
taskDelayMilliseconds - How many milliseconds to delay the actual write operation to optimize for batching (default 1 ms).
queueLimit - Gets or sets the limit on the number of requests in the lazy writer thread request queue (default 10000).
overflowAction - Gets or sets the action to be taken when the lazy writer thread request queue count exceeds the set limit (default Discard).
The v2 of the library applies better default values for common usage. Check your current configuration and compare it against the breaking changes below to adapt to your use case.
compressionLevel set to optimal by default, instead of noCompression. If you are certain you do no want compression,
specify the value as noCompression in your configuration.orderWrites set to false by default. Set it to true only if you run on Grafana Loki v2.3 or lower, or if your logs come largely unordered
and need to be reordered for Grafana Loki to process them.The configuration example above structures log messages and event properties in JSON. This allows parsing messages in Grafana Loki and then applying advanced filtering and querying.
The following query would filter log messages which have a property JobName and a value of A
in Grafana Loki:
{app="my-app-name"}
| json
| JobName="A"
These raw messages would look like the following in Grafana Loki :
{ "level": "Info ", "source": "Program", "message": "Executing job \"A\"", "JobName": "A" }
See Log Queries and Parser Expressions in Loki for more details.
See NLog.Loki.Benchmarks for benchmark between HTTP and gRPC clients for NLog targets for Grafana Loki.
| 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 is compatible. 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 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 is compatible. 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. |
Showing the top 5 NuGet packages that depend on NLog.Targets.Loki:
| Package | Downloads |
|---|---|
|
RavenDB.Cloud.Api.Client
This client library enables working with RavenDB Cloud API. For reference see: - RavenDB Cloud : https://cloud.ravendb.net - RavenDB Cloud Swagger UI : https://api.cloud.ravendb.net/swagger/index.html - RavenDB Cloud Swagger JSON : https://api.cloud.ravendb.net/api/v1/swagger.json |
|
|
Adnc.Shared.WebApi
This layer implements shared classes and middleware for authentication, authorization, exception handling, and service component registration. It is the shared layer for all microservice WebApi layers and is required by each of them. |
|
|
Snow.Gqz.Shared.WebApi
1、修复CPU占用率过高问题,_waitMillisecond 由3 调整为 1000。 2、caching模块代码优化调整 |
|
|
JRY.Library.Base
Package Description |
|
|
JRY.Library
Package Description |
Showing the top 1 popular GitHub repositories that depend on NLog.Targets.Loki:
| Repository | Stars |
|---|---|
|
AlphaYu/adnc
A pragmatic .NET 8 framework for modular monoliths to evolve seamlessly into distributed microservices.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.5.0 | 26,416 | 3/26/2026 |
| 2.4.1 | 4,764 | 3/16/2026 |
| 2.4.0 | 52,372 | 12/9/2025 |
| 2.3.3 | 755 | 12/8/2025 |
| 2.3.2 | 604 | 12/7/2025 |
| 2.3.1 | 463 | 12/7/2025 |
| 2.3.0 | 456 | 12/7/2025 |
| 2.2.1 | 12,250 | 12/1/2025 |
| 2.2.0 | 929,584 | 4/25/2024 |
| 2.1.2 | 33,172 | 3/9/2024 |
| 2.1.1 | 143,333 | 8/25/2023 |
| 2.1.0 | 10,642 | 8/10/2023 |
| 2.0.0 | 15,088 | 7/30/2023 |
| 1.5.1 | 29,065 | 7/30/2023 |
| 1.5.0 | 26,363 | 7/30/2023 |
| 1.4.6 | 136,634 | 11/27/2022 |
| 1.4.5 | 32,616 | 10/22/2022 |
| 1.4.4 | 141,270 | 6/11/2022 |
| 1.4.3 | 73,176 | 5/26/2022 |
| 1.4.2 | 2,522 | 5/23/2022 |