/nlog-kafka-target

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

Primary LanguageC#Apache License 2.0Apache-2.0

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.5 - 4.8

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"
            clientId="${hostname}"
            async="false"
            >

    </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}
brokers :string yes* Kafka brokers with comma-separated
clientId :layout no Producer Client Identification. Ex. ${hostname}
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

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"
            >

    </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 
            
        }
    }
}