title: Configure Custom Layout Renderer of nLog in .NET CORE 3.1 Web API Project
body: <p>I have configured nLog in my .NET CORE 3.1 Web API project which is all working fine. I want to use Custom Layout Renderer  ${hello-world} in nLog but unable to view result in Log file. Not sure what I am missing from the puzzle. <a href="https://nlog-project.org/2015/06/30/extending-nlog-is-easy.html" rel="nofollow noreferrer">https://nlog-project.org/2015/06/30/extending-nlog-is-easy.html</a></p>
  <p>do I need the following configuration in my code?</p>
  <pre><code>ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition(&quot;hello-world&quot;, typeof(HelloWorldLayoutRenderer));
  </code></pre>
  <h2>Program.cs</h2>
  <pre><code>public static void Main(string[] args)
      {
          var logger = NLog.Web.NLogBuilder.ConfigureNLog(&quot;nlog.config&quot;).GetCurrentClassLogger();
  
          try
          {
              logger.Debug(&quot;init main&quot;);
              CreateHostBuilder(args).Build().Run();
          }
          catch (Exception exception)
          {
              logger.Error(exception, &quot;Stopped program because of exception&quot;);
              throw;
          }
          finally
          {
              NLog.LogManager.Shutdown();
          }    
      }
  
      public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .UseServiceProviderFactory(new AutofacServiceProviderFactory())
              .ConfigureLogging((hostingContext, logging) =&gt;
               {
                   logging.ClearProviders();
                   logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
               })
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
              })
          .UseNLog();
  }
  </code></pre>
  <h2>Service Configuration</h2>
  <pre><code>services.AddSingleton&lt;ILoggerService, LoggerService&gt;();
  </code></pre>
  <h2>DI</h2>
  <pre><code> containerBuilder.RegisterType&lt;LoggerService&gt;().As&lt;ILoggerService&gt;();
  </code></pre>
  <h2>NLog.Config</h2>
  <pre><code>&lt;target xsi:type=&quot;File&quot; name=&quot;allfile&quot; fileName=&quot;..\logs\nlog-all-${shortdate}.log&quot;
          layout=&quot;${longdate}| ${hello-world}&quot;&gt; &lt;/target&gt;
  
  &lt;target xsi:type=&quot;File&quot; name=&quot;ownFile-web&quot; fileName=&quot;..\logs\nlog-own-${shortdate}.log&quot;
          layout=&quot;${longdate}| ${environment: USERNAME}| ${hello-world}&quot;&gt;&lt;/target&gt;
  
  &lt;rules&gt;
   &lt;logger name=&quot;*&quot; minlevel=&quot;Trace&quot; writeTo=&quot;allfile&quot; /&gt;
   &lt;logger name=&quot;Microsoft.*&quot; maxlevel=&quot;Info&quot; final=&quot;true&quot; /&gt;
   &lt;logger name=&quot;*&quot; minlevel=&quot;Trace&quot; writeTo=&quot;ownFile-web&quot; /&gt;    
  &lt;/rules&gt;
  </code></pre>
  <h2>LayoutRanderer class</h2>
  <pre><code> [LayoutRenderer(&quot;hello-world&quot;)]
  public class HelloWorldLayoutRenderer : LayoutRenderer
  {
      public string Config1 { get; set; }
  
      [RequiredParameter]
      public string Config2 { get; set; }
  
      [DefaultParameter]
      public bool Caps { get; set; }
  
      protected override void Append(StringBuilder builder, LogEventInfo logEvent)
      {
          builder.Append(&quot;Hello World!&quot;);
      }
  }
  </code></pre>
  <h2>LoggerService Class</h2>
  <pre><code> public class LoggerService: ILoggerService
  {
      private static ILogger logger = LogManager.GetCurrentClassLogger();
  
      private const string LoggerName = &quot;NLogLogger&quot;;
  
      public void LogDebug(string message)
      {
          logger.Debug(message);
      }
  
      public void LogError(string message)
      {
          logger.Error(message);
      }
  
      public void LogInfo(string message)
      {
          logger.Info(message);
      }
  
      public void LogWarn(string message)
      {
          logger.Warn(message);
      }
  }
  </code></pre>
  <h2>Web API Controller</h2>
  <pre><code> [ApiController]
  public class DashboardController : ControllerBase
  {
      private readonly ILoggerService Logger;
  
      public DashboardController(
          ILoggerService _logger
          )
      {
           Logger = _logger;
           Logger.LogDebug(&quot;NLog injected into HomeController.....&quot;);
  
      }
  
    /// test logs 
    [HttpGet]
      public ActionResult&lt;HelloMessage&gt; GetMessage()
      {
          Logger.LogInfo(&quot;Here is info message from the controller.&quot;);
          Logger.LogDebug(&quot;Here is debug message from the controller.&quot;);
          Logger.LogWarn(&quot;Here is warn message from the controller.&quot;);
          Logger.LogError(&quot;Here is error message from the controller.&quot;);
       }
  </code></pre>