title: NLog custom LayoutRenderer - json: can't get working
body: <p>I'm new to NLog, and mostly it's very simple to setup. </p>
  
  <p>The problem I'm having is setting up a custom LayoutRenderer</p>
  
  <p>JsonLayoutRenderer.cs (namespace: NBT.Logging; separate Project within the same solution)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Xml;
  using Newtonsoft.Json.Linq;
  using NLog;
  using NLog.LayoutRenderers;
  
  namespace NBT.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);
  
          }
      }
  }
  </code></pre>
  
  <p>Code taken from <a href="https://gist.github.com/caevyn/9594522" rel="noreferrer">https://gist.github.com/caevyn/9594522</a></p>
  
  <p>NLog.config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"&gt;
  
      &lt;extensions&gt;
          &lt;add assembly="NLog.Targets.Redis"/&gt;
          &lt;add assembly="NBT.Logging" /&gt;
      &lt;/extensions&gt;
  
    &lt;targets async="true"&gt;
        &lt;target xsi:type="Redis" name="redis" host="127.0.0.1" key="logstash" dataType="channel" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|j|${json}"/&gt;
    &lt;/targets&gt;
  
    &lt;rules&gt;
      &lt;logger name="*" minlevel="Info" writeTo="redis" /&gt;
    &lt;/rules&gt;
  &lt;/nlog&gt;
  </code></pre>
  
  <p>So the logging to redis show all the normal layout items but not the ${json} bit</p>
  
  <p><code>"2014-05-17 12:36:58.7480|INFO|ExampleController|Index loaded|j|"</code></p>
  
  <p>Probably missing something simple.</p>
  
  <p>Update (Added register layoutRenderer to Global.asax.cs)</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {
  
          AreaRegistration.RegisterAllAreas();
  
          WebApiConfig.Register(GlobalConfiguration.Configuration);
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          AuthConfig.RegisterAuth();
  
          // Register custom Model Binders
          ModelBinderConfig.RegisterModelBinders();
  
          ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("json", typeof(JsonLayoutRenderer));
  
      }
  </code></pre>