title: Custom NLog Layout Renderer Produces No Output In Some Projects
body: <p>.NET Framework 4.8, NLog 4.7.6</p>
  <p>I have implemented two simple <code>LayoutRenderer</code> subclasses which just write a Guid (fetched from Web context or a static <code>Stack&lt;T&gt;</code>). These are working great in two Web projects, but they are not working in a third Web app nor in a couple of console apps/services. Configuration is consistent between apps, but some work and some don't.</p>
  <p>Example:</p>
  <pre><code>[LayoutRenderer(&quot;request-guid&quot;)]
  public class RequestGuidLayoutRenderer:
      LayoutRenderer
  {
      protected override void Append(StringBuilder builder, LogEventInfo logEvent)
      {
          var guid = RequestIdentityService.Current.RequestGuid?.ToString() ?? Constants.NullSession;
          builder.Append(guid);
      }
  }
  </code></pre>
  <p>There is also a <code>session-guid</code> layout renderer that is almost identical, except for getting the <code>SessionGuid</code> instead of the <code>RequestGuid</code>.</p>
  <p>My config section looks like this, where MyAssemblyName contains the layout renderers:</p>
  <pre><code>&lt;nlog autoReload=&quot;true&quot;&gt;
    &lt;extensions&gt;
      &lt;assembly name=&quot;MyAssemblyName&quot; /&gt;
    &lt;/extensions&gt;
    &lt;variable name=&quot;brief&quot; value=&quot;${longdate} ${session-guid} ${request-guid} ${level} ${replace:searchFor=^.*\.([^.]+)$:replaceWith=$1:regex=true:inner=${logger}} ${message}${onexception:inner= ${exception:format=toString,Data}}&quot; /&gt;
    &lt;variable name=&quot;verbose&quot; value=&quot;${longdate} | ${machinename} | ${processid} | ${processname} | ${session-guid} ${request-guid} | ${level} | ${logger} | ${message}${onexception:inner= ${exception:format=toString,Data}}&quot; /&gt;
    &lt;targets&gt;
      &lt;target name=&quot;all_in&quot; type=&quot;File&quot; layout=&quot;${verbose}&quot; fileName=&quot;C:/log/appx/current.txt&quot; archiveFileName=&quot;C:/log/appx/archive/{#}.txt&quot; archiveNumbering=&quot;DateAndSequence&quot; archiveAboveSize=&quot;10485760&quot; archiveDateFormat=&quot;yyyyMMdd&quot; keepFileOpen=&quot;false&quot; maxArchiveFiles=&quot;100&quot; encoding=&quot;iso-8859-2&quot; /&gt;
      &lt;target name=&quot;LogStreaming&quot;  type=&quot;Trace&quot; layout=&quot;${verbose}&quot; /&gt;
    &lt;/targets&gt;
    &lt;rules&gt;
      &lt;logger name=&quot;*&quot; minlevel=&quot;Debug&quot; writeTo=&quot;all_in&quot; /&gt;
      &lt;logger name=&quot;*&quot; minlevel=&quot;Error&quot; writeTo=&quot;LogStreaming&quot; /&gt;
    &lt;/rules&gt;
  &lt;/nlog&gt;
  </code></pre>
  <p>To try to ensure that these layout renderers are registered, I am also registering them at the beginning of <code>Application_Start()</code> for the Web apps, and in <code>Main()</code> or equivalent in other app types.</p>
  <pre><code>    public static void Register()
      {
          LayoutRenderer.Register&lt;RequestGuidLayoutRenderer&gt;(&quot;request-guid&quot;);
          LayoutRenderer.Register&lt;SessionGuidLayoutRenderer&gt;(&quot;session-guid&quot;);
  
          LogManager.GetCurrentClassLogger().Error($&quot;Registered NLog LayoutRenderers request-guid and session-guid.&quot;);
      }
  </code></pre>
  <p>For two Web apps this is working flawlessly, but for a third it is producing no output for these layout renderers. Internal logs confirm that it's having trouble finding them, or perhaps it's trying to use them before they have been registered.</p>
  <pre><code>Error parsing layout session-guid will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'session-guid'
     at NLog.Config.Factory`2.CreateInstance(String itemName)
     at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name, Nullable`1 throwConfigExceptions)
  
  Error parsing layout request-guid will be ignored. Exception: System.ArgumentException: LayoutRenderer cannot be found: 'request-guid'
     at NLog.Config.Factory`2.CreateInstance(String itemName)
     at NLog.Layouts.LayoutParser.GetLayoutRenderer(ConfigurationItemFactory configurationItemFactory, String name, Nullable`1 throwConfigExceptions)
  </code></pre>
  <p>I discovered accidentally that if I set <code>throwConfigExceptions=&quot;true&quot;</code> in config then it suddenly WORKS in the Web app where it otherwise doesn't! But why? And is there an alternative way to fix it so that I don't have to risk runtime errors from logging breaking our apps?</p>