NLog.Targets.OpenTelemetryHttp
6.2.3
dotnet add package NLog.Targets.OpenTelemetryHttp --version 6.2.3
NuGet\Install-Package NLog.Targets.OpenTelemetryHttp -Version 6.2.3
<PackageReference Include="NLog.Targets.OpenTelemetryHttp" Version="6.2.3" />
<PackageVersion Include="NLog.Targets.OpenTelemetryHttp" Version="6.2.3" />
<PackageReference Include="NLog.Targets.OpenTelemetryHttp" />
paket add NLog.Targets.OpenTelemetryHttp --version 6.2.3
#r "nuget: NLog.Targets.OpenTelemetryHttp, 6.2.3"
#:package NLog.Targets.OpenTelemetryHttp@6.2.3
#addin nuget:?package=NLog.Targets.OpenTelemetryHttp&version=6.2.3
#tool nuget:?package=NLog.Targets.OpenTelemetryHttp&version=6.2.3
NLog.Targets.OpenTelemetryHttp
NLog OpenTelemetry target for exporting log events to an OpenTelemetry Collector or any OTLP/HTTP-compatible endpoint.
Designed for applications that already use NLog and want to export log events directly using the OTLP protobuf format, without depending on the OpenTelemetry SDK logging pipeline.
The target automatically captures TraceId and SpanId for distributed tracing correlation and exports NLog event properties as structured OpenTelemetry log attributes.
If you have trouble with output, check NLog InternalLogger for clues. See also Troubleshooting NLog.
See also the OpenTelemetry specifications for OTLP/HTTP and exporters.
Register Extension
The OpenTelemetry type alias is available in an NLog.config file after registering the extension:
<extensions>
<add assembly="NLog.Targets.OpenTelemetryHttp"/>
</extensions>
Alternatively, register it in code using the fluent configuration API:
LogManager.Setup().SetupExtensions(ext => {
ext.RegisterTarget<NLog.Targets.OpenTelemetryHttpTarget>();
});
Configuration Examples
The typical OTLP/HTTP endpoint URL is http://localhost:4318/v1/logs. The target also reads OpenTelemetry environment variables as fallbacks.
NLog.config
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Targets.OpenTelemetryHttp"/>
</extensions>
<targets>
<target xsi:type="OpenTelemetry"
name="otel"
url="http://localhost:4318/v1/logs" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="otel" />
</rules>
</nlog>
AppSettings.json
When using NLog.Extensions.Logging the target can be configured from appsettings.json:
{
"NLog": {
"extensions": [
{ "assembly": "NLog.Targets.OpenTelemetryHttp" }
],
"targets": {
"otel": {
"type": "OpenTelemetry",
"url": "http://localhost:4318/v1/logs"
}
},
"rules": [
{ "logger": "*", "minLevel": "Info", "writeTo": "otel" }
]
}
}
Fluent Configuration API
NLog.LogManager.Setup().LoadConfiguration(builder =>
{
builder.ForLogger()
.FilterMinLevel(NLog.LogLevel.Info)
.WriteTo(new NLog.Targets.OpenTelemetryHttpTarget
{
Url = "http://localhost:4318/v1/logs"
});
});
Parameters
| Parameter | Default | Description |
|---|---|---|
| url | OTEL_EXPORTER_OTLP_LOGS_ENDPOINT |
OTLP/HTTP endpoint URL. Falls back to OTEL_EXPORTER_OTLP_ENDPOINT, with /v1/logs appended to the generic endpoint. |
| layout | ${message} |
Layout used to populate the OpenTelemetry LogRecord.Body field. |
| traceId | ${activity:property=TraceId} |
Layout used to populate the OpenTelemetry LogRecord.TraceId field. |
| spanId | ${activity:property=SpanId} |
Layout used to populate the OpenTelemetry LogRecord.SpanId field. |
| includeEventProperties | true |
Includes NLog event properties as OpenTelemetry log attributes. |
| maxRecursionDepth | 2 |
Max nested depth when serializing dictionaries and collections as structured OpenTelemetry values. |
| maxItemsPerCollection | 1000 |
Max number of items serialized from an individual collection. Additional items are omitted. |
| scopeName | NLog |
OpenTelemetry instrumentation scope name. |
| serviceName | ${appdomain:format=Friendly} |
OpenTelemetry service.name resource attribute. |
| serviceVersion | ${assembly-version:Default=} |
OpenTelemetry service.version resource attribute. |
| hostName | ${hostname} |
OpenTelemetry host.name resource attribute. |
| resourceAttributes | OTEL_RESOURCE_ATTRIBUTES | Additional OpenTelemetry resource attributes. |
| headers | OTEL_EXPORTER_OTLP_LOGS_HEADERS |
Additional HTTP headers. Falls back to OTEL_EXPORTER_OTLP_HEADERS. |
| Batching and Retry | Default | Description |
|---|---|---|
| batchSize | 200 |
Maximum number of log events to send in a single HTTP payload. |
| compress | None |
Optional compression of the OTLP/HTTP request payload. Supports None, GZip, and GZipFast. |
| maxPayloadSizeBytes | 40960 |
Maximum payload size in bytes before splitting into multiple HTTP requests when using batchSize. |
| taskDelayMilliseconds | 50 |
Delay before processing queued log events. Increasing value can improve batching. |
| taskTimeoutSeconds | 150 |
Maximum lifetime in seconds for the entire task performing the HTTP request. |
| retryCount | 3 |
Number of retry attempts for failed write operations. |
| retryDelayMilliseconds | 2500 |
Initial delay before retry after failed request. Delay doubles for each retry. |
| queueLimit | 10000 |
Maximum number of pending log events allowed in the internal queue. |
| overflowAction | Discard |
Action taken when the internal queue reaches its limit (Grow / Block / Discard). |
| Network and Security | Default | Description |
|---|---|---|
| sendTimeoutSeconds | 30 |
HTTP request timeout in seconds. |
| sslCertificateFile | Client certificate file used for mutual TLS authentication. | |
| sslCertificatePassword | Password for the client certificate file. | |
| sslCertificateThumbprint | Thumbprint of a client certificate from X509Store (CurrentUser, then LocalMachine). Alternative to sslCertificateFile. |
|
| proxyUrl | Proxy server URL. | |
| proxyUser | Proxy authentication username. | |
| proxyPassword | Proxy authentication password. |
Resource Attributes
Additional OpenTelemetry resource attributes can be configured using resourceAttribute entries:
<target xsi:type="OpenTelemetry" name="otel">
<resourceAttribute name="service.namespace" layout="Backend" />
<resourceAttribute name="deployment.environment" layout="Production" />
</target>
Structured Log Properties
NLog event properties are exported as structured OpenTelemetry log attributes using the OTLP AnyValue format:
- Primitive values: strings, Booleans, integers, and floating-point numbers.
- Enum values: serialized as strings.
- DateTime values:
DateTimeandDateTimeOffsetserialized usingToString("o")for round-trip ISO 8601 format. - Dictionary values:
IDictionaryandIEnumerable<KeyValuePair<string, object>>represented as OpenTelemetry KeyValueList values. - Arrays and collections: serialized as OpenTelemetry ArrayValue, with default max of 1000 items, but can be adjusted with setting
maxItemsPerCollection - Nested collections: Max Recursion Depth of default two, but can be adjusted with setting
maxRecursionDepth.
Unknown or unrecognized object types fall back to string formatting. Values beyond the supported nesting depth are formatted as strings. String values longer than 65,536 characters are truncated.
Azure Application Insights
Microsoft Application Insights ver. 3.x supports log ingestion using the OpenTelemetry Protocol (OTLP).
NLog.Targets.OpenTelemetryHttp supports custom HTTP headers, allowing NLog to send logs directly to Application Insights using HTTP authentication headers:
<target xsi:type="OpenTelemetry" name="applicationInsights"
url="https://your-app-insights-endpoint/v1/logs">
<header name="x-otlp-api-key" layout="${configsetting:name=ApplicationInsights.InstrumentationKey}" />
</target>
Note The official approach for sending logs and telemetry to Application Insights is to use: builder.Services.AddOpenTelemetry().UseAzureMonitor(opt => ...).
OpenTelemetry Environment Variables
The target automatically resolves standard OpenTelemetry environment variables when equivalent target properties have not been explicitly configured.
Endpoint
Order of precedence:
OTEL_EXPORTER_OTLP_LOGS_ENDPOINTOTEL_EXPORTER_OTLP_ENDPOINT
When OTEL_EXPORTER_OTLP_ENDPOINT is used, /v1/logs is automatically appended.
Headers
Order of precedence:
OTEL_EXPORTER_OTLP_LOGS_HEADERSOTEL_EXPORTER_OTLP_HEADERS
Example:
OTEL_EXPORTER_OTLP_HEADERS=api-key=my-secret-key
Compression
Order of precedence:
OTEL_EXPORTER_OTLP_LOGS_COMPRESSIONOTEL_EXPORTER_OTLP_COMPRESSION
Supported values:
gzipnone
Timeout
Order of precedence:
OTEL_EXPORTER_OTLP_LOGS_TIMEOUTOTEL_EXPORTER_OTLP_TIMEOUT
Timeout values are specified in milliseconds.
Service Name
OTEL_SERVICE_NAME
Overrides the service.name value from OTEL_RESOURCE_ATTRIBUTES.
Resource Attributes
OTEL_RESOURCE_ATTRIBUTES
Example:
OTEL_RESOURCE_ATTRIBUTES=service.namespace=Backend,deployment.environment=Production
The special attribute:
service.name
is automatically mapped to the serviceName property.
Client Certificate
Order of precedence:
OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATEOTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE
Used to configure mutual TLS authentication.
| 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 is compatible. 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 is compatible. 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. |
-
.NETFramework 4.7.1
- NLog (>= 6.2.2)
- NLog.Targets.HttpClient (>= 6.2.2)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
-
.NETStandard 2.0
- NLog (>= 6.2.2)
- NLog.Targets.HttpClient (>= 6.2.2)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
-
.NETStandard 2.1
- NLog (>= 6.2.2)
- NLog.Targets.HttpClient (>= 6.2.2)
- System.Diagnostics.DiagnosticSource (>= 8.0.0)
-
net10.0
- NLog (>= 6.2.2)
- NLog.Targets.HttpClient (>= 6.2.2)
-
net8.0
- NLog (>= 6.2.2)
- NLog.Targets.HttpClient (>= 6.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Support override of MaxRecursionDepth (Default: 2) + MaxItemsPerCollection (Default: 1000)