title: AWS Lambda writes extra line in cloudwatch (.NET Core 3.1)
body: <p>I am programming a lambda function in .NET Core 3.1(C#) and I want to leave log of the actions being performed in my logic, when I deploy my lambda in the AWS Lambda service in cloudwatch it adds an extra line that is only displayed in the output (also affects my local but not my plain text file) what could I be misconfiguring?</p>
  <p>To create the project use the template &quot;AWS Lambda Project (.NET Core - C#)&quot; from the AWS Toolkit For Visual Studio 2019.</p>
  <p>Dependencies:</p>
  <pre><code>NLog.Web.AspNetCore (4.14.0)
  NLog (4.7.13)
  Microsoft.Extensions.Configuration.Abstractions (6.0.0)
  Amazon.Lambda.Serialization.SystemTextJson (2.2.0)
  Amazon.Lambda.Core (2.1.0)
  </code></pre>
  <p>Repository: <a href="https://github.com/Weyne/AWS-NET_CORE-NLOG/" rel="nofollow noreferrer">https://github.com/Weyne/AWS-NET_CORE-NLOG/</a></p>
  <p>NLog.config</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;nlog xmlns=&quot;http://www.nlog-project.org/schemas/NLog.xsd&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
          &gt;
  
      &lt;targets&gt;
          &lt;target name=&quot;aws&quot;
                  xsi:type=&quot;AWSTarget&quot;
                  logGroup=&quot;aws/lambda/WeyneLoggingWithNLog&quot;
                  region=&quot;us-east-1&quot;
                  layout=&quot;
                 -------------- ${level} (${longdate}) --------------${newline}
                  ${newline}
                  Call Site: ${callsite}${newline}
                  Exception Type: ${exception:format=Type}${newline}
                  Exception Message: ${exception:format=Message}${newline}
                  Stack Trace: ${exception:format=StackTrace}${newline}
                  Additional Info: ${message}${newline}&quot; /&gt;
          &lt;target xsi:type=&quot;File&quot; name=&quot;fileTarget&quot; filename=&quot;C:\Log\${machinename}-${local-ip}-mylambda-${date:format=yyyy-MM-dd}.txt&quot; layout=&quot;${longdate}|${level:uppercase=true}|${local-ip}|${callsite}|${message}|${exception:format=tostring}&quot;&gt;&lt;/target&gt;
  
          &lt;target name=&quot;aws&quot; type=&quot;AWSTarget&quot; /&gt;
      &lt;/targets&gt;
      &lt;rules&gt;
          &lt;!--Skip Microsoft logs and so log only own logs--&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;fileTarget&quot; /&gt;
          &lt;logger minlevel=&quot;Error&quot; name=&quot;*&quot; writeTo=&quot;aws&quot;/&gt;
      &lt;/rules&gt;
  
  &lt;/nlog&gt;
  </code></pre>
  <p>Function.cs</p>
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      using System.Threading.Tasks;
      
      using Amazon.Lambda.Core;
      using Microsoft.AspNetCore.Mvc.Infrastructure;
      using Microsoft.Extensions.Configuration;
      using Microsoft.Extensions.DependencyInjection;
      using Microsoft.Extensions.DependencyInjection.Extensions;
      using Microsoft.Extensions.Hosting;
      using Microsoft.Extensions.Logging;
      using NLog;
      using NLog.Web;
      using WeyneLoggingWithNLog.Interfaces;
      using WeyneLoggingWithNLog.Services;
      
      // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
      [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
      
      namespace WeyneLoggingWithNLog
      {
          public class Function
          {
      
              /// &lt;summary&gt;
              /// A simple function that takes a string and does a ToUpper
              /// &lt;/summary&gt;
              /// &lt;param name=&quot;input&quot;&gt;&lt;/param&gt;
              /// &lt;param name=&quot;context&quot;&gt;&lt;/param&gt;
              /// &lt;returns&gt;&lt;/returns&gt;
              public string FunctionHandler(string input, ILambdaContext context)
              {
                  var logger = NLogBuilder.ConfigureNLog(&quot;NLog.config&quot;).GetCurrentClassLogger();
                  try
                  {
      
                      var builder = new ConfigurationBuilder();
                      BuildConfig(builder);
      
                      var host = Host.CreateDefaultBuilder()
                          .ConfigureLogging((hostingContext, logging) =&gt;
                          {
                              logging.ClearProviders(); //esta línea hace la diferencia
                              logging.AddConsole();//mal
                              logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //esta línea hace la diferencia
                              logging.AddNLogWeb();
                          })
                          .ConfigureServices((context, services) =&gt;
                          {
                              services.AddHttpContextAccessor();
                              services.TryAddSingleton&lt;IActionContextAccessor, ActionContextAccessor&gt;();
                              services.AddScoped&lt;IProcessService, ProcessService&gt;();
                              services.BuildServiceProvider();
                              services.AddLogging();
                          })
                          .UseNLog()
                          .Build();
      
                      var service = ActivatorUtilities.CreateInstance&lt;ProcessService&gt;(host.Services);
      
                      return service.Invoke(input).Result.ToUpper();
                  }
                  catch (Exception ex)
                  {
                      logger.Error(ex, &quot;Stopped program because of exception&quot;);
                      throw ex;
                  }
                  finally
                  {
                      NLog.LogManager.Shutdown();
                  }
              }
      
              static void BuildConfig(IConfigurationBuilder builder)
              {
                  builder.SetBasePath(Directory.GetCurrentDirectory())
                      //.AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
                      .AddEnvironmentVariables();
              }
          }    
      }
  </code></pre>
  <p>ProcessService.cs</p>
  <pre><code>    using System.Threading.Tasks;
      using WeyneLoggingWithNLog.Interfaces;
      using Microsoft.Extensions.Logging;
      
      
      namespace WeyneLoggingWithNLog.Services
      {
          public class ProcessService : IProcessService
          {
              private readonly ILogger&lt;ProcessService&gt; _logger;
      
              public ProcessService(ILogger&lt;ProcessService&gt; logger)
              {
                  _logger = logger;
              }
      
              public async Task&lt;string&gt; Invoke(string input)
              {
                  _logger.LogInformation(&quot;Hola mundo: {0}&quot;, input);
                  _logger.LogError(&quot;Error: {0}&quot;, input);
                  _logger.LogTrace(&quot;Trace: {0}&quot;, input);
                  return input;
              }
          }
      }
  </code></pre>
  <p>The log printing on my local
  <a href="https://i.stack.imgur.com/yjEiB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yjEiB.png" alt="local registration" /></a></p>
  <p>I have noticed that if I remove the &quot;logging.AddConsole();&quot; line from Function.cs, the extra line (marked in yellow) is no longer displayed but the Log ouput stops working (in my local and in aws) and only prints the physical log.</p>
  <p><a href="https://i.stack.imgur.com/hNp68.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hNp68.png" alt="local log output" /></a></p>
  <p>cloudwatch:
  <a href="https://i.stack.imgur.com/8KF1B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8KF1B.png" alt="enter image description here" /></a></p>
  <p>aws lambda:
  <a href="https://i.stack.imgur.com/mLs8G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mLs8G.png" alt="enter image description here" /></a></p>