NLog.Targets.KafkaAppender 2.1.7

dotnet add package NLog.Targets.KafkaAppender --version 2.1.7                
NuGet\Install-Package NLog.Targets.KafkaAppender -Version 2.1.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NLog.Targets.KafkaAppender" Version="2.1.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NLog.Targets.KafkaAppender --version 2.1.7                
#r "nuget: NLog.Targets.KafkaAppender, 2.1.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NLog.Targets.KafkaAppender as a Cake Addin
#addin nuget:?package=NLog.Targets.KafkaAppender&version=2.1.7

// Install NLog.Targets.KafkaAppender as a Cake Tool
#tool nuget:?package=NLog.Targets.KafkaAppender&version=2.1.7                

nlog-kafka-target

NLog appender for kafka which provides the custom topics pattern and partitions

Nuget GitHub issues Nuget GitHub forks GitHub stars

Supported frameworks

- .NET 5, 6, 7 and 8
- .NET Core 2 and 3
- .NET Standard 2.0+
- .NET Framework 4.6.2 - 4.8.1

Getting Started

Step 1: Install NLog.Targets.KafkaAppender package from nuget.org

Install via Package-Manager   Install-Package NLog.Targets.KafkaAppender
Install via .NET CLI          dotnet add package NLog.Targets.KafkaAppender

Step 2: Configure nlog sections

<?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">
  <extensions>
    <add assembly="NLog.Targets.KafkaAppender"/>
  </extensions>
  <targets>
    <target xsi:type="KafkaAppender"
            name="kafka"
            topic="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true}"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
            brokers="localhost:9092"
            async="false">
        <setting key="client.id" value="NLog_${machinename}" /> 
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="kafka" />
  </rules>
</nlog>
Param Name Variable Type Requirement Description Default
name :string yes* Target's name
topic :layout yes* Topic pattern can be layout ${logger}
layout :layout no Layout used to format log messages. ${longdate}|${level:uppercase=true}|${logger}|${message}
brokers :string yes* Kafka brokers with comma-separated
async :boolean no Async or sync mode false
securityProtocol :enum no Broker Security Protocol. Ex. Plaintext,Ssl,SaslPlaintext,SaslSsl plaintext
SaslMechanism :enum no SASL Mechanism. Ex. Gssapi,Plain,ScramSha256,ScramSha512,OAuthBearer Gssapi
sslCertificateLocation :string no Path to SSL certificate
sslCaLocation :string no Path to SSL CA certificate
sslKeyLocation :string no Path to SSL key file
sslKeyPassword :string no SSL key password
SaslUsername :string no Simple Authentication and Security Layer (SASL) Username
SaslPassword :string no Simple Authentication and Security Layer (SASL) Password
messageTimeoutMs :int no Limits the time a produced message waits for successful delivery
setting key + value no Kafka Producer Config

Check documentation about all Layout Renderers

SASL Basic authentication

Scenario: using Confluent Cloud http://developer.confluent.io/get-started/dotnet/#configuration

<?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">
  <extensions>
    <add assembly="NLog.Targets.KafkaAppender"/>
  </extensions>
  <targets>
    <target xsi:type="KafkaAppender"
            name="kafka"
            topic="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true}"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
            brokers="localhost:9092"
            async="false"
            securityProtocol="SaslPlaintext"
            SaslMechanism="Plain"
            SaslUsername="API-Key"
            SaslPassword="API-secret">
        <setting key="client.id" value="NLog_${machinename}" /> 
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="kafka" />
  </rules>
</nlog>

Usage

using System;

namespace NLog.Targets.KafkaAppender.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();
            logger.Error("hello world");
            
            //topic layout:     ${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true}
            //message layout:   ${longdate}|${level:uppercase=true}|${logger}|${message}
            
            //topic output:     NLog.Targets.KafkaAppender.Test.Program.Main
            //message output:   2018-12-05 18:27:46.7382|ERROR|NLog.Targets.KafkaAppender.Test.Program|hello world 
            
        }
    }
}

Product 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. 
.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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.7 103 11/16/2024
2.1.6 13,103 5/30/2024
2.1.5 6,585 3/26/2024
2.1.4 26,806 10/15/2023
2.1.3 5,298 8/16/2023
2.1.2 2,791 7/24/2023
2.1.1 6,813 7/9/2023
2.1.0 171 7/6/2023
2.0.0 79,616 5/16/2022
1.0.8 19,230 9/21/2020
1.0.7 1,692 7/21/2020
1.0.6 8,417 12/6/2019
1.0.5 34,994 8/2/2019
1.0.4 744 5/1/2019
1.0.3 2,514 12/7/2018
1.0.2 673 12/5/2018
1.0.1 660 12/5/2018