NLog.Targets.Http 1.0.20

dotnet add package NLog.Targets.Http --version 1.0.20
NuGet\Install-Package NLog.Targets.Http -Version 1.0.20
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.Http" Version="1.0.20" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NLog.Targets.Http --version 1.0.20
#r "nuget: NLog.Targets.Http, 1.0.20"
#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.Http as a Cake Addin
#addin nuget:?package=NLog.Targets.Http&version=1.0.20

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

NLog.Targets.HTTP

Nuget (with prereleases)

NLog.Targets.HTTP is an HTTP POST target for NLog. When combined with JSON formatter it can be used to send events to an instance of Splunk and other HTTP based collectors.

This target is inherently asynchronous, with performance on par or better than with the AsyncWrapper. Remember to Flush and to give it enough time to complete.

Note that the async="true" attribute applied to <targets > will discard by default.

Getting started

Add the library as an extension to nlog:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <extensions>
    <add assembly="NLog.Targets.Http" />
  </extensions>
  <targets>
    ...

Available Configuration Parameters

Listed below are available configuration parameters with their default values (where applicable)

<target name='target name' 
        type='HTTP' 
        URL='protocol://server:port/path'
        Method='POST'
        Authorization='phrase token' 
        BatchSize='1'
        MaxQueueSize='2147483647'
        IgnoreSslErrors='true'
        FlushBeforeShutdown='true'
        ContentType='application/json'
        Accept='application/json'
        DefaultConnectionLimit='2'
        Expect100Continue='false'
        UseNagleAlgorithm='true'
        ConnectTimeout='30000' 
        InMemoryCompression='true'
        ProxyUrl=''
        ProxyUser=''
        ProxyPassword=''
    >
    
    <header name='key' value='value'/>
    
    
</target>
URL

The URL (Layout element) to send messages to (mandatory).

Method

HTTP method to use (GET,POST).

Authorization

The Authorization Header value to pass.

BatchSize

Number of messages to be sent together in one call.

BatchAsJsonArray

If set to true, messages will be packaged as JSON Array instead of being separated with Environment.NewLine character. Default is false.

MaxQueueSize

Maximum number of messages awaiting to be send. Please note, that if this value is set too low, the logger might be blocking.

IgnoreSslErrors

Some SSL certificates might be invalid or not-trusted.

FlushBeforeShutdown

Force all messages to be delivered before shutting down. Note that by design .Net apps are limited to about 2 seconds to shutdown.
Make sure you leverage LogManager.Flush(TimeSpan.FromHours(24)) in most extreme scenarios.

ContentType

HTTP ContentType Header value. Default is application/json.

Accept

HTTP Accept Header value. Default is application/json.

DefaultConnectionLimit

How many connections might be used at the same time. Changes ServicePointManager.DefaultConnectionLimit, which might affect other parts of your system. Performance improvement was noticeable even with 16 connections, but your application might require more for other functionality. Use with caution.

Expect100Continue

See this article.

UseNagleAlgorithm

The Nagle algorithm is used to buffer small packets of data and transmit them as a single packet. This process, referred to as "nagling," is widely used because it reduces the number of packets transmitted and lowers the overhead per packet. The Nagle algorithm is fully described in IETF RFC 896.

ConnectTimeout

How long should the client wait to connect (default is 30 seconds).

InMemoryCompression

Reduces the amount of memory consumed at the expense of increased CPU usage. Significant performance improvement can be achieved by using default of false.

ProxyUrl

Designates a proxy server to use. Must include protocol (http|https) and port. It is a Layout element so can be dynamic.

ProxyUser

If proxy authentication is needed, you can specify it with a domain prefix, i.e. DOMAIN\USER.

ProxyPassword

Password to use for proxy authentication.

Additional Headers

Additional HTTP Headers can be specified by adding multiple <header name='..' value='..'/> elements. Elements with a blank name or value will not be included.

UnixTimeLayoutRenderer

The "Unix Time" renderer supports universalTime option (boolean), just like the date renderer does.

<attribute name='unixutc' layout='${unixtime:universalTime=true}' />

Sample SPLUNK 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" >
  <targets>
    <target name='splunk' 
            type='HTTP' 
            URL='https://localhost:8088/services/collector/event'
            Authorization='Splunk d758f3fa-740f-4bb7-96be-3da4128708bc' 
            BatchSize='100'>
      <layout type='JsonLayout'>
        <attribute name='sourcetype' layout='_json' />
        <attribute name='host' layout='${machinename}' />
        <attribute name='event' encode='false'>
          <layout type='JsonLayout'>
            <attribute name='level' layout='${level:upperCase=true}' />
            <attribute name='source' layout='${logger}' />
            <attribute name='thread' layout='${threadid}' />
            <attribute name='message' layout='${message}' />
            <attribute name='utc' layout='${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss.fff}' />
          </layout>
        </attribute>
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="splunk" />
  </rules>
</nlog>

Sample stats

On a Lenovo Xeon E3-1505M v6 powered laptop with 64GB of RAM and 3GB/s NVMe storage, this HTTP target was able to consistenlty accept about 125,000 messages per second, and 62,500 per second received and processed by local Dockerized Splunk Enterpise 8.2.4. Please note, that these stats depend heavily on the message size, batch size, and amount of bytes that can be submitted in a single POST message.

Running local Splunk
docker pull splunk/splunk:latest
docker run -d -p 8000:8000 -p 8088:8088 -e "SPLUNK_START_ARGS=--accept-license" -e "SPLUNK_PASSWORD=Pass@W0rd" --name splunk splunk/splunk:latest

After a few moments, depeneding on your systems capacity, login to Splunk at http://localhost:8080/ with admin and Pass@W0rd. The HttpEventCollector (HEC) will listen on port 8088 once created from Settings - Data Inputs menu option.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  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
1.0.20 144,898 1/2/2022
1.0.19 272 1/2/2022
1.0.18 620 1/2/2022
1.0.17 284 1/2/2022
1.0.16 8,251 10/30/2021
1.0.10 2,109 10/5/2021
1.0.9 323 10/5/2021
1.0.8 1,335 9/20/2021
1.0.7 9,795 4/14/2021
1.0.6 352 4/14/2021
1.0.5 411 4/9/2021
1.0.4 3,957 2/11/2021
1.0.3 346 2/10/2021
1.0.2 5,971 9/18/2020
1.0.1 54,193 11/6/2019
1.0.0 956 10/3/2019
0.0.8 525 10/1/2019
0.0.7 469 9/30/2019
0.0.6 458 9/30/2019
0.0.5 471 9/26/2019
0.0.4 488 9/11/2019
0.0.3 499 8/30/2019
0.0.2 521 8/30/2019
0.0.1-PreRelease 396 8/30/2019

Version 1.0.20
     - Pre-emptively addressing layout rendering related bugs
     Version 1.0.19
     - Reduced memory stress and improved performance by 25%
     Version 1.0.18
     - Bug fixes and performance improvements
     Version 1.0.17
     - Bug fix for HTTP error flagg never reset
     - Minor code optimization
     Version 1.0.16
     - Bug fix related to composing HTTP payload
     - New feature to package messages as JSON array instead of separating by new line.
     - Memory compression disabled by default
     Version 1.0.14
     - Bug fix for Flush
     Version 1.0.13
     - await/async fixes
     Version 1.0.12
     - Minor potential bug fix for use of null event
     Version 1.0.11
     - Added support for Layout element in ProxyUser, ProxyPassword, Authorization and custom Headers value.
     Version 1.0.10
     - ProxyUrl has been converted to a Layout element and can now be dynamic
     Version 1.0.9
     - Added support for additional headers
     Version 1.0.8
     - Brought back support for .Net Standard 2.0
     Version 1.0.7
     - Bug fix #17 for .Net Core
     Version 1.0.5
     - Wraped url parameter with Layout to allow config settings assignment
     - Flush error event, Keepalive support, error timeout, refactor
     Version 1.0.4
     - Signed assemblies
     - Added packaging for .Net Core 3.1 and .Net 5.0
     Version 1.0.3
     - Updated to latest NLog version
     - Added universalTime option to unixtime renderer
     Version 1.0.2
     - minor performance improvements
     - code refactoring
     - updated documentation
     - sample command-line app for code profiling
     Version 1.0.1
     - Refactored for a single HttpClient as HttpWebRequest was creating too many connection in TIMED_WAIT state under load.
     - Upgraded .Net Standard to 2.1 and introduced support for Core 3.0 and Framework 4.8
     Version 1.0.0
     - Official release
     Version 0.0.8
     - added support for Nagle algorithm.
     Version 0.0.7
     - pulling default properties from ServicePointManager
     Version 0.0.6
     - switching to default properties and changing behavior only of the selected ServicePoint
     Version 0.0.5
     - code flow optimizations
     Version 0.0.4
     - Changed Expect100Continue to the default value of true
     Version 0.0.3
     - Added support for proxy server configuration.
     Version 0.0.2
     - Thanks to Alan Barber (https://github.com/AlanBarber) for NuGet build scripts and inspiration for this Http Target (his Splunk target https://github.com/AlanBarber/NLog.Targets.Splunk).
     - Thanks to Nate McMaster (https://natemcmaster.com/blog/2017/03/09/vs2015-to-vs2017-upgrade/) and Derek Comartin (https://codeopinion.com/converting-a-library-to-netstandard/) for conversion tips.
     Version 0.0.1
     - Big thanks to Stan Smith for insiting to make it lean and performant.
     - First publish to NuGet.