Skip to content

Instantly share code, notes, and snippets.

@dbl4ck
Last active March 13, 2019 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbl4ck/7e392efd8357a1f422ca7d115b068234 to your computer and use it in GitHub Desktop.
Save dbl4ck/7e392efd8357a1f422ca7d115b068234 to your computer and use it in GitHub Desktop.
Using NLog + Logz.IO in Powershell (using Logzio.DotNet.NLog)

Using this solution, the following should be acquired from nuget and placed in the script directory.

  • Logzio.DotNet.Core.dll
  • Logzio.DotNet.NLog.dll
  • Newtonsoft.Json.dll
  • NLog.config
  • NLog.dll
$scriptPath = $MyInvocation.MyCommand.Path
$scriptPath = Split-Path $scriptPath
[Reflection.Assembly]::LoadFile("$scriptPath\NLog.dll") | Out-Null
# Load Logzio.DotNet.NLog and it's dependencies.
[Reflection.Assembly]::LoadFile("$scriptPath\Logzio.DotNet.NLog.dll") | Out-Null
[Reflection.Assembly]::LoadFile("$scriptPath\Logzio.DotNet.Core.dll") | Out-Null
[Reflection.Assembly]::LoadFile("$scriptPath\Newtonsoft.Json.dll") | Out-Null
## Force transport layer security to 1.2 to ensure compatibility with logz.io listener endpoint.
[System.Net.ServicePointManager]::SecurityProtocol =
[System.Net.SecurityProtocolType]::Tls12
## Load config file.
$ne = New-Object NLog.Config.XmlLoggingConfiguration("$scriptPath\NLog.config")
([NLog.LogManager]::Configuration) = $ne
# Declare class logger, and off to work we go!
$logger = [NLog.LogManager]::GetCurrentClassLogger()
$logger.Warn("This is a warning")
$logger.Error("This is an error")
$logger.Info("This is informational")
# HACK: Delay a couple seconds to give the buffer a chance to send to Logz.io
## This should be a bit more than "bufferTimeout" in the config.
Start-Sleep -Seconds 3
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Trace" internalLogFile="C:\Users\andre\Documents\PS.NlogNation\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<extensions>
<add assemblyFile="C:\my\script\directory\Logzio.DotNet.NLog.dll"/>
</extensions>
<targets>
<target name="logzio" type="Logzio"
token="<your-token>"
logzioType="nlog"
listenerUrl="https://listener.logz.io:8071"
bufferSize="100"
bufferTimeout="00:00:01"
retriesMaxAttempts="3"
retriesInterval="00:00:02"
debug="false">
<contextproperty name="timestamp" layout="${longdate}" />
<contextproperty name="level" layout="${level:upperCase=true}"/>
<contextproperty name="logger" layout="${logger}"/>
<contextproperty name="pid" layout="${processid}"/>
<contextproperty name="tid" layout="${threadid}" />
<contextproperty name="identity" layout="${windows-identity}"/>
<contextproperty name="host" layout="${machinename}"/>
<contextproperty name="exception" layout="${replace-newlines:${exception:format=Type,Data:maxInnerExceptionLevel=10}}" />
<contextproperty name="message" layout="${replace-newlines:${message}}"/>
<contextproperty name="callsite" layout="${replace-newlines:${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true}}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logzio" enabled="true" />
</rules>
</nlog>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment