Last active
August 29, 2015 13:57
-
-
Save caevyn/9594522 to your computer and use it in GitHub Desktop.
Json layout renderer for nlog + rabbitMQ target + logstash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Text; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using NLog; | |
using NLog.LayoutRenderers; | |
namespace Cats.Middleware.Logging | |
{ | |
[LayoutRenderer("json")] | |
public class JsonLayoutRenderer : LayoutRenderer | |
{ | |
protected override void Append(StringBuilder builder, LogEventInfo logEvent) | |
{ | |
dynamic logEntry = new JObject(); | |
logEntry.TimeStamp = logEvent.TimeStamp.ToString("yyyy-MM-ddTHH:mm:ss.mmmzzz", CultureInfo.InvariantCulture); | |
logEntry.TimeStampUtc = logEvent.TimeStamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.mmmZ", CultureInfo.InvariantCulture); | |
logEntry.Level = logEvent.Level.ToString(); | |
logEntry.LoggerName = logEvent.LoggerName; | |
logEntry.Message = logEvent.FormattedMessage; | |
foreach (var prop in logEvent.Properties) | |
{ | |
logEntry[prop.Key.ToString()] = prop.Value.ToString(); | |
} | |
var json = logEntry.ToString(Formatting.None); | |
builder.Append(json); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input { | |
rabbitmq { | |
durable => true | |
exchange => "logstash" | |
codec => json | |
host => "localhost" | |
passive => false | |
password => "guest" | |
port => 5672 | |
prefetch_count => 1 | |
ssl => false | |
# tags => ... # array (optional) | |
type => "nlog" | |
user => "guest" | |
verify_ssl => false | |
vhost => "/" | |
} | |
} | |
filter{ | |
date{ | |
match => ["TimeStampUtc", "ISO8601"] | |
} | |
} | |
output { | |
stdout { codec => rubydebug } | |
elasticsearch { embedded => true } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nlog> | |
<extensions> | |
<add assembly="NLog.Targets.RabbitMQ" /> | |
<add assembly="Cats.Middleware.Logging" /> | |
</extensions> | |
<targets async="true"> | |
<target name="RabbitMQTarget" | |
type="RabbitMQ" | |
username="guest" | |
password="guest" | |
hostname="localhost" | |
exchange="logstash" | |
port="5672" | |
topic="logstash" | |
vhost="/" | |
durable="true" | |
appid="Test" | |
maxBuffer="10240" | |
heartBeatSeconds="3" | |
useJSON="false" | |
layout="${json}" | |
/> | |
<target name="file" type="File" fileName="${basedir}/logging/loggy.txt" layout="${json}" /> | |
</targets> | |
<rules> | |
<logger name="*" minlevel="Debug" writeTo="RabbitMQTarget,file" /> | |
</rules> | |
</nlog> |
Hey, sorry I missed your comment. You have it sorted now?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Having problems implementing a similar thing for my own project. ${json} never gets populated.
Is there anything else, that needs to be in place?
I asked the question on stackoverflow; I re-namespaced it to suite.
http://stackoverflow.com/questions/23710935/nlog-custom-layoutrenderer-json-cant-get-working