LogDB.NLog 5.1.1

dotnet add package LogDB.NLog --version 5.1.1
                    
NuGet\Install-Package LogDB.NLog -Version 5.1.1
                    
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="LogDB.NLog" Version="5.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LogDB.NLog" Version="5.1.1" />
                    
Directory.Packages.props
<PackageReference Include="LogDB.NLog" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LogDB.NLog --version 5.1.1
                    
#r "nuget: LogDB.NLog, 5.1.1"
                    
#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.
#:package LogDB.NLog@5.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LogDB.NLog&version=5.1.1
                    
Install as a Cake Addin
#tool nuget:?package=LogDB.NLog&version=5.1.1
                    
Install as a Cake Tool

LogDB.NLog

Package Version: 5.1.1 | README Updated: 2026-03-31

NLog target for LogDB - Send your NLog logs directly to LogDB with full structured logging support, filtering, and batching.

Installation

dotnet add package LogDB.NLog

Quick Start

XML Configuration

<nlog>
  <extensions>
    <add assembly="LogDB.NLog" />
  </extensions>

  <targets>
    <target name="logdb"
            xsi:type="LogDB"
            apiKey="your-api-key"
            defaultApplication="MyApp"
            defaultEnvironment="Production"
            defaultPayloadType="Log" />
  </targets>

  <rules>
    <logger name="*" minLevel="Info" writeTo="logdb" />
  </rules>
</nlog>

Code-Based Configuration

using NLog;
using NLog.Config;
using LogDB.NLog;

var config = new LoggingConfiguration();

var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultApplication = "MyApp",
    DefaultEnvironment = "Production",
    DefaultPayloadType = LogDBPayloadType.Log
};

config.AddTarget("logdb", logDBTarget);
config.AddRule(LogLevel.Info, LogLevel.Fatal, "logdb");

LogManager.Configuration = config;

var logger = LogManager.GetCurrentClassLogger();
logger.Info("Hello from NLog to LogDB!");

Tenant scope is resolved from the API key on the server side.

Basic Usage

Simple Configuration

var config = new LoggingConfiguration();
var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultPayloadType = LogDBPayloadType.Log
};
config.AddTarget("logdb", logDBTarget);
config.AddRule(LogLevel.Info, LogLevel.Fatal, "logdb");
LogManager.Configuration = config;

Advanced Configuration

var config = new LoggingConfiguration();

var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultApplication = "MyApp",
    DefaultEnvironment = "Production",
    DefaultCollection = "app-logs",
    DefaultPayloadType = LogDBPayloadType.Log,
    MinimumLevel = LogLevel.Info,

    // Batching options
    EnableBatching = true,
    BatchSize = 100,
    FlushIntervalSeconds = 5,

    // Reliability options
    MaxRetries = 3,
    EnableCircuitBreaker = true,
    EnableCompression = true,

    // Custom filter
    Filter = logEvent =>
        logEvent.Level >= LogLevel.Warning ||
        logEvent.Properties.ContainsKey("Important")
};

config.AddTarget("logdb", logDBTarget);
config.AddRule(LogLevel.Info, LogLevel.Fatal, "logdb");
config.AddRule(LogLevel.Warn, LogLevel.Fatal, "logdb", "Microsoft.*");

LogManager.Configuration = config;

Cache + Beat Routing (Event Properties)

Payload type is mandatory. Set a global default payload type on the target, then override per-event when needed.

Control properties:

  • LogDBType: LogDBPayloadType.Log | LogDBPayloadType.Cache | LogDBPayloadType.Beat
  • Shared: LogDBCollection

Cache properties:

  • LogDBCacheKey (required)
  • LogDBCacheValue (optional, falls back to log message)
  • LogDBTtlSeconds (optional)

Beat properties:

  • LogDBMeasurement (required)
  • LogDBApplication (optional)
  • LogDBEnvironment (optional)
  • LogDBTags (optional dictionary)
  • LogDBFields (optional dictionary)
  • Prefix alternatives: Tag.<key>, Field.<key>
// Global default payload type for regular logs
var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultPayloadType = LogDBPayloadType.Log
};

// Cache write through NLog
var cacheEvent = new LogEventInfo(LogLevel.Info, "Demo", "cache upsert");
cacheEvent.Properties["LogDBType"] = LogDBPayloadType.Cache;
cacheEvent.Properties["LogDBCacheKey"] = "session:user-42";
cacheEvent.Properties["LogDBCacheValue"] = "active";
cacheEvent.Properties["LogDBTtlSeconds"] = 600;
logger.Log(cacheEvent);

// Beat write through NLog
var beatEvent = new LogEventInfo(LogLevel.Info, "Demo", "heartbeat");
beatEvent.Properties["LogDBType"] = LogDBPayloadType.Beat;
beatEvent.Properties["LogDBMeasurement"] = "worker_health";
beatEvent.Properties["Tag.worker"] = "worker-1";
beatEvent.Properties["Field.status"] = "ok";
beatEvent.Properties["Field.cpu"] = 23.4;
logger.Log(beatEvent);

Structured Logging

All NLog properties are automatically mapped to LogDB attributes:

var logEvent = new LogEventInfo(LogLevel.Info, "MyLogger", "User {UserId} logged in from {IpAddress}");
logEvent.Properties["UserId"] = 12345;
logEvent.Properties["IpAddress"] = "192.168.1.1";
logger.Log(logEvent);

// Maps to:
// - AttributesS["UserId"] = "12345" (or AttributesN if numeric)
// - AttributesS["IpAddress"] = "192.168.1.1"

Property Type Mapping

  • StringsAttributesS
  • Numbers (int, long, double, etc.) → AttributesN
  • BooleansAttributesB
  • DateTime/DateTimeOffsetAttributesD
  • Complex objects → JSON string in AttributesS
  • Collections → JSON string in AttributesS

Exception Handling

Exceptions are automatically captured with full stack traces:

try
{
    // Your code
}
catch (Exception ex)
{
    var logEvent = new LogEventInfo(LogLevel.Error, "MyLogger", "An error occurred");
    logEvent.Exception = ex;
    logger.Log(logEvent);

    // Automatically maps:
    // - Exception = exception type name
    // - StackTrace = full stack trace
    // - AttributesS["ExceptionMessage"] = exception message
}

Correlation and Tracing

The target automatically extracts correlation IDs and trace information:

// Using properties
var logEvent = new LogEventInfo(LogLevel.Info, "MyLogger", "Processing request");
logEvent.Properties["CorrelationId"] = correlationId;
logEvent.Properties["TraceId"] = traceId;
logEvent.Properties["SpanId"] = spanId;
logger.Log(logEvent);
// Maps to Log.CorrelationId and AttributesS

// Activity/Trace support
using var activity = Activity.StartActivity("ProcessOrder");
var activityEvent = new LogEventInfo(LogLevel.Info, "MyLogger", "Order processed");
// Automatically extracts TraceId, SpanId, ParentSpanId from Activity.Current
logger.Log(activityEvent);

HTTP Context

Extract HTTP context information:

var logEvent = new LogEventInfo(LogLevel.Info, "MyLogger", "Request completed");
logEvent.Properties["RequestPath"] = "/api/orders";
logEvent.Properties["HttpMethod"] = "POST";
logEvent.Properties["StatusCode"] = 200;
logEvent.Properties["IpAddress"] = "192.168.1.1";
logEvent.Properties["UserId"] = 12345;
logEvent.Properties["UserEmail"] = "user@example.com";
logger.Log(logEvent);

// Maps to:
// - RequestPath
// - HttpMethod
// - StatusCode
// - IpAddress
// - UserId
// - UserEmail

Configuration Options

Option Default Description
ApiKey (required) LogDB API key for authentication
ServiceUrl auto-discover LogDB service URL
DefaultApplication null Default application name
DefaultEnvironment "production" Default environment
DefaultCollection "logs" Default collection name
DefaultPayloadType null Explicit default payload type when LogDBType is not set per-event
MinimumLevel Info Minimum log level to send
EnableBatching true Enable log batching
BatchSize 100 Number of logs per batch
FlushIntervalSeconds 5 Maximum wait before flushing (seconds)
EnableCompression true Compress payloads
MaxRetries 3 Maximum retry attempts
EnableCircuitBreaker true Enable circuit breaker
Filter null Custom log filter function
FallbackTarget null Fallback target if LogDB fails

Tenant scope is resolved from the provided API key automatically.

XML Configuration

Basic XML Config

<nlog>
  <extensions>
    <add assembly="LogDB.NLog" />
  </extensions>

  <targets>
    <target name="logdb"
            xsi:type="LogDB"
            apiKey="${logdb:apikey}"
            defaultApplication="MyApp"
            defaultEnvironment="${environment}"
            defaultCollection="app-logs"
            defaultPayloadType="Log"
            minimumLevel="Info"
            enableBatching="true"
            batchSize="100"
            flushIntervalSeconds="5" />
  </targets>

  <rules>
    <logger name="Microsoft.*" minLevel="Warning" writeTo="logdb" />
    <logger name="*" minLevel="Info" writeTo="logdb" />
  </rules>
</nlog>

Advanced XML Config with Filtering

<nlog>
  <extensions>
    <add assembly="LogDB.NLog" />
  </extensions>

  <targets>
    <target name="console" xsi:type="Console" />

    <target name="logdb"
            xsi:type="LogDB"
            apiKey="${logdb:apikey}"
            defaultApplication="MyApp"
            defaultPayloadType="Log"
            enableBatching="true"
            batchSize="100"
            maxRetries="3"
            enableCircuitBreaker="true"
            enableCompression="true" />
  </targets>

  <rules>
    
    <logger name="Microsoft.*" minLevel="Warning" writeTo="logdb" />

    
    <logger name="*" minLevel="Info" writeTo="logdb" />
  </rules>
</nlog>

Custom Filtering

Programmatic Filter

var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultPayloadType = LogDBPayloadType.Log,
    Filter = logEvent =>
    {
        // Only send logs with specific properties
        return logEvent.Properties.ContainsKey("Important") ||
               logEvent.Level >= LogLevel.Warning;
    }
};

XML-Based Filtering

NLog's built-in rule system provides powerful filtering:

<rules>
  
  <logger name="MyApp.Services.*" minLevel="Error" writeTo="logdb" />

  
  <logger name="*" minLevel="Warn" writeTo="logdb" />
</rules>

Integration with ASP.NET Core

// Program.cs
using NLog;
using NLog.Web;

var builder = WebApplication.CreateBuilder(args);

// Configure NLog
LogManager.Configuration = new NLogLoggingConfiguration(builder.Configuration.GetSection("NLog"));

var app = builder.Build();

// Use ILogger<T> in your controllers/services
app.MapGet("/", (ILogger<Program> logger) => {
    logger.LogInformation("Hello from ASP.NET Core!");
    return "Hello World";
});

app.Run();

<nlog>
  <extensions>
    <add assembly="LogDB.NLog" />
  </extensions>

  <targets>
    <target name="logdb"
            xsi:type="LogDB"
            apiKey="${configsetting:item=LogDB:ApiKey}"
            defaultApplication="MyWebApp"
            defaultPayloadType="Log"
            defaultEnvironment="${configsetting:item=ASPNETCORE_ENVIRONMENT}" />
  </targets>

  <rules>
    <logger name="Microsoft.*" minLevel="Warning" writeTo="logdb" />
    <logger name="*" minLevel="Info" writeTo="logdb" />
  </rules>
</nlog>

Performance

  • Batching: Logs are automatically batched for efficient transmission
  • Async: All log sending is asynchronous and non-blocking
  • Circuit Breaker: Automatic protection against service failures
  • Compression: Payloads are compressed to reduce bandwidth

Error Handling

The target handles errors gracefully:

var logDBTarget = new LogDBTarget
{
    ApiKey = "your-api-key",
    DefaultPayloadType = LogDBPayloadType.Log,
    FallbackTarget = new FileTarget("fallback")
    {
        FileName = "logs/fallback.log"
    }
};

// If LogDB fails, logs will be written to fallback target

Examples

Multiple Targets

<nlog>
  <extensions>
    <add assembly="LogDB.NLog" />
  </extensions>

  <targets>
    <target name="console" xsi:type="Console" />
    <target name="file" xsi:type="File" fileName="logs/app.log" />
    <target name="logdb"
            xsi:type="LogDB"
            apiKey="your-api-key" />
  </targets>

  <rules>
    <logger name="*" minLevel="Info" writeTo="console,file,logdb" />
  </rules>
</nlog>

Using Extension Methods

using LogDB.NLog;

var config = new LoggingConfiguration();
config.AddLogDBTargetWithRule(
    "logdb",
    "your-api-key",
    LogDBPayloadType.Log,
    LogLevel.Info,
    LogLevel.Fatal,
    target => {
        target.DefaultApplication = "MyApp";
        target.DefaultEnvironment = "Production";
    }
);

LogManager.Configuration = config;

Property Type Mapping

NLog properties are automatically mapped to LogDB attribute types:

  • StringsAttributesS
  • Numbers (int, long, double, etc.) → AttributesN
  • BooleansAttributesB
  • DateTime/DateTimeOffsetAttributesD
  • Complex objects → JSON string in AttributesS
  • Collections → JSON string in AttributesS

Comparison with Serilog

Feature Serilog Sink NLog Target
Configuration Fluent API XML or Code-based
Filtering Custom function Rules + Custom function
Property Model Typed (LogEventPropertyValue) Boxed (object)
Async Built-in WriteAsyncTask()
Batching Via client Via client
Retry/Circuit Breaker Via client Via client

Both implementations use the same underlying LogDBClient for sending logs, ensuring consistent behavior.

License

MIT License - see LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 Framework net472 is compatible.  net48 was computed.  net481 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
5.1.1 110 3/31/2026

v5.1.1 - NuGet.org Release
- Aligns LogDB.NLog with LogDB.Client 5.1.1 for nuget.org publishing
- Keeps package metadata aligned with docs and publish pipeline
- No breaking API changes

v5.0.18 - Version Line Alignment
- Aligns NLog package with LogDB.Client and Serilog 5.0.18 line
- No API shape changes

v5.0.17 - Mandatory Payload Type + Enum Routing
- Added public LogDBPayloadType enum (Log, Cache, Beat)
- LogDBType routing is now explicit: missing type no longer silently defaults to Log
- Added LogDBTarget.DefaultPayloadType for explicit global default when desired
- Updated README and examples to use enum-based routing

v5.0.16 - Version Line Alignment
- Aligns NLog package version with active 5.x release train so package managers treat it as latest
- Includes Cache + Beat property-based routing

v1.0.4 - Cache + Beat Routing
- Added property-based routing to LogCache and LogBeat via LogDBType markers
- Added support for LogDBCacheKey/Value/Ttl and LogDBMeasurement/Tags/Fields
- Updated docs and integration examples for Cache/Beat routing

v1.0.1 - Packaging Fixes
- Fixed NuGet dependency metadata to include LogDB.Client for runtime
- Removed unintended dependency on LogDB.Shared

v1.0.0 - Initial Release
- NLog target for LogDB
- Full support for structured logging
- Automatic property mapping to LogDB attributes
- Exception handling and stack trace support
- Correlation ID and activity tracking
- Custom filtering support
- Batching and retry support via LogDB client
- Async target support