title: NLog how to log messages to console with different JSON layout formats in different scenarios
body: <p>We are working with NLog. we need to log messages to console with different JSON formats in different scenarios.</p>
  
  <p><strong>Ex: If exception occurred</strong> Below is the format</p>
  
  <pre><code>{
              “id”:”5656”,
              “uuid”:”xdd895-65454”,
              “key”:”somekeyname”,
              “message”: “** exception message **”,
              “stack_trace”: “”**exception stack trace**”
  }
  </code></pre>
  
  <p><strong>If Normal log message</strong></p>
  
  <pre><code>{
              "id":"8898998"
              “messge”: “Time taken to retrieve data from database”
              “Time”: “1100” (in milli seconds)
  }
  </code></pre>
  
  <p>Like this we have 10 scenarios.</p>
  
  <p><strong>Approach 1:</strong>
  I tried creating different targets in web.config file in nlog section as follows</p>
  
  <pre><code> &lt;target xsi:type="Console" name="console_exception" &gt;
      &lt;layout xsi:type="JsonLayout"&gt;
        &lt;attribute name="id" layout="${mdlc:itemid}" /&gt;
        &lt;attribute name="uuid" layout="${mdlc:item=uuid}" /&gt;
        &lt;attribute name="loglevel" layout="${level}" /&gt;
        &lt;attribute name="timestamp" layout="${date}" /&gt;
        &lt;attribute name="message" layout="${message}" /&gt;
      &lt;/layout&gt;
    &lt;/target&gt;
    &lt;target xsi:type="Console" name="console_databaseTimeTaken" &gt;
      &lt;layout xsi:type="JsonLayout"&gt;
        &lt;attribute name="id" layout="${mdlc:item=id}" /&gt;
        &lt;attribute name ="custommessage" layout="Time taken to retrieve data from database"/&gt;
        &lt;attribute name="timestamp" layout="${date}" /&gt;
        &lt;attribute name="message" layout="${message}" /&gt;
      &lt;/layout&gt;
    &lt;/target&gt;
    &lt;rules&gt;
    &lt;logger name="*" minLevel="Trace" writeTo ="console"/&gt;
  &lt;/rules&gt;
  </code></pre>
  
  <p>I do not know how to set the target and layout programmatically. 
  So far I have found conditional based logging but, that is not a good approach.</p>
  
  <p><strong>Approach 2:</strong>
  When logging the message we manually format the string message as per the need and then calling the log method</p>
  
  <pre><code>string logmessag= string.Format("\"id\":\"{0}\",\"messge\": {1}\"\",\"Time\": \"{2}(in milli seconds)\"", id, message, time);
  </code></pre>
  
  <p>Technologies used: Webapi2, Nlog 4.0.3
  IDE: VS2017
  Let us know the best approach to solve this problem
  Thanks in advance</p>