GelfLayout-package contains custom layout renderer for NLog to format log messages as GELF Json structures.
PM> Install-Package NLog.GelfLayout
- IncludeAllProperties - Include all properties from the LogEvent. Boolean. Default = true
- IncludeMldc - Include all properties from NLog MDLC / MEL BeginScope. Boolean. Default = false
- ExcludeProperties - Comma separated string with LogEvent property names to exclude.
- IncludeLegacyFields - Include deprecated fields no longer part of official GelfVersion 1.1 specification. Boolean. Default = true
- Facility - Graylog Facility. Ignored when IncludeLegacyFields=False
You can configure this layout for NLog Targets that respect Layout attribute. For instance the following configuration writes log messages to a RabbitMQ-adolya Exchange in GELF format.
<?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" >
<extensions>
<add assembly="NLog.Targets.RabbitMQ" />
<add assembly="NLog.Layouts.GelfLayout" />
</extensions>
<targets async="true">
<target name="RabbitMQTarget"
xsi:type="RabbitMQ"
hostname="mygraylog.mycompany.com"
exchange="logmessages-gelf"
durable="true"
useJSON="false"
layout="${gelf:facility=MyFacility}"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="RabbitMQTarget" />
</rules>
</nlog>
In this example there would be a Graylog2 server that consumes the queued GELF messages.
<?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" >
<extensions>
<add assembly="NLog.Layouts.GelfLayout" />
</extensions>
<targets async="true">
<target xsi:type="Network" name="GelfHttp" address="http://localhost:12201/gelf" layout="${gelf:facility=MyFacility}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="GelfHttp" />
</rules>
</nlog>
<?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" >
<extensions>
<add assembly="NLog.Layouts.GelfLayout" />
</extensions>
<targets async="true">
<target xsi:type="Network" name="GelfTcp" address="tcp://graylog:12200" layout="${gelf:facility=MyFacility}" newLine="true" lineEnding="Null" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="GelfTcp" />
</rules>
</nlog>
<?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" >
<extensions>
<add assembly="NLog.Layouts.GelfLayout" />
</extensions>
<targets async="true">
<target xsi:type="Network" name="GelfHttp" address="http://localhost:12201/gelf">
<layout type="GelfLayout" facility="MyFacility">
<field name="threadid" layout="${threadid}" />
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="GelfHttp" />
</rules>
</nlog>
GELF converter module is all taken from Gelf4NLog by Ozan Seymen