Here is a guide on how to send .NET application logs to Coralogix by using NLog framework. The main focus of this guide is to integrate an OpenTelemetry .NET SDK to NLog framework by implementing a target called NLog.Targets.OpenTelemetryProtocol.
Package dependencies setup
First step, you need to add the package NLog.Targets.OpenTelemetryProtocol to your project, apart from NLog:
Here is how your app's csproj file can look like. Please make sure to update the packages to the latest version if you'll use this example for testing.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="NLog.Targets.OpenTelemetryProtocol" Version="1.0.4" />
</ItemGroup>
<ItemGroup>
<None Update="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Create NLog target
Here is how you configure NLog target in NLog.config to send logs to OpenTelemetry endpoint.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Debug"
internalLogToConsole="true"
internalLogFile="internal-nlog.txt">
<extensions>
<add assembly="NLog.Targets.OpenTelemetryProtocol" />
</extensions>
<targets>
<!-- Console target for debugging -->
<target xsi:type="Console"
name="console"
layout="${longdate} ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}" />
<!-- OTLP target -->
<target xsi:type="OtlpTarget"
name="otlp"
endpoint="${environment:OTEL_EXPORTER_OTLP_ENDPOINT}"
usehttp="false"
servicename="HelloWorldApp"
includeEventProperties="true"
includeScopeProperties="true"
includeFormattedMessage="true"
scheduledDelayMilliseconds="500"
maxExportBatchSize="10" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="console,otlp" />
</rules>
</nlog>
Application example
Let's implement the Hello World application in Program.cs as an example of how to send logs using log4net library:
using System;
using NLog;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
// Configure NLog using the new API
LogManager.Setup().LoadConfigurationFromFile("NLog.config");
// Create an NLog logger
var log = LogManager.GetCurrentClassLogger();
// Log messages at different levels
log.Info("Hello, World! This is an info message from NLog.");
log.Debug("This is a debug message.");
log.Warn("This is a warning message.");
log.Error("This is an error message.");
// Log some structured information
log.Info("Application started at {0}", DateTime.Now);
log.Info("Testing OpenTelemetry log shipment via NLog OpenTelemetryProtocol target");
// Flush all pending logs
LogManager.Flush();
// Keep the application running for a bit to ensure logs are sent
// The target uses a batch exporter with scheduledDelayMilliseconds=500
Console.WriteLine("\nWaiting for logs to be exported...");
System.Threading.Thread.Sleep(2000);
// Flush again and shutdown NLog
Console.WriteLine("Flushing logs...");
LogManager.Flush(TimeSpan.FromSeconds(5));
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Shutting down NLog...");
LogManager.Shutdown();
Console.WriteLine("Logging complete. Check your OpenTelemetry collector for the logs.");
}
}
}
Logging output
With OpenTelemetry SDK, we can send logs from the application either to local OpenTelemetry Collector setup or directly to Coralogix, using an OpenTelemetry endpoint.
OpenTelemetry Collector
Once we configure the environment variable OTEL_EXPORTER_OTLP_ENDPOINT, setting it to the OpenTelemetry collector's endpoint, we should be able to see application logs coming to the target collector, from where we can also send logs to Coralogix.
Coralogix OpenTelemetry endpoint
Once we configure the environment variables, setting it to the Coralogix OpenTelemetry endpoint with authentication header, application and subsystem name, we should be able to see application logs coming to Coralogix.
For setting up authentication, make sure to use the Send-Your-Data API key.
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingress.:443
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer send_your_data_key" \
OTEL_RESOURCE_ATTRIBUTES="cx.application.name=AppName, cx.subsystem.name=SubName"
Additional resources
| Coralogix Endpoints | Coralogix Endpoints |
Support
Need help?
Our world-class customer success team is available 24/7 to walk you through your setup and answer any questions that may come up.
Feel free to reach out to us via our in-app chat or by sending us an email at support@coralogix.com.

