aws-logging-dotnet: NLog not sending logs to CloudWatch

NLog not sending logs to CloudWatch

Target frameword <TargetFramework>netcoreapp1.0</TargetFramework>

Packages

<PackageReference Include="AWS.Logger.Core" Version="1.1.4" />
<PackageReference Include="AWS.Logger.NLog" Version="1.1.4" />
<PackageReference Include="NLog" Version="5.0.0-beta07" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
<PackageReference Include="Amazon.Lambda.Logging.AspNetCore" Version="1.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.0.1" />
...

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true"
      internalLogToConsole="true"
      internalLogLevel="Trace">
  <extensions> <add assembly="NLog.AWS.Logger" /> </extensions>
  <targets>
    
    <target name="awsout" type="AWSTarget" logGroup="critical-errors" region="eu-central-1"> 
      <layout xsi:type="JsonLayout" includeAllProperties="true"> 
        <attribute name="time" layout="${longdate}" /> 
        <attribute name="level" layout="${level:upperCase=true}"/> 
        <attribute name="message" layout="${message}" encode="false" /> 
        <attribute name="stack-trace" layout="${stacktrace:format=Raw:topFrames=5}"/> 
      </layout> 
    </target>
    </targets>
  <rules>
    <logger name="CriticalErrors" minlevel="Info" writeTo="awsout" />
  </rules>
</nlog>

Code

Logger Logger = LogManager.GetLogger("CriticalErrors");
Logger.Error(() => JsonConvert.SerializeObject(new Dictionary<string, object>
            {
                {"UNKNOWN-ERROR", internalError.GetLogCode()},
                {"ERROR-TYPE", "UNKNOWN"},
                {"PAYLOAD", stackTrace}
            }));

What am I doing wrong?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 30 (14 by maintainers)

Most upvoted comments

@jstallm @clearwaterstream Thanks for the update. I will take a note of this and discuss with developer to review the comment on the blog https://aws.amazon.com/blogs/developer/amazon-cloudwatch-logs-and-net-logging-frameworks/.

Hi @ntrivix,

Good morning.

I was going through the issue backlog and came across this guidance question. As @norm pointed to Readme https://github.com/aws/aws-logging-dotnet#aws-lambda, there is a note mentioning that NLog might not be suitable for use with Lambda since NLog sends and processes messages in background thread. The background thread will be frozen once a Lambda event is processed and may not ever be unfrozen if more Lambda events are not received for some time. Hence, with Lambda, it is recommended to use either the ILambdaContext.Logger.LogLine or the Amazon.Lambda.Logging.AspNetCore package.

Please let me know if this issue could be closed.

Thanks, Ashish

It does say in the README for this repo that these libraries are not recommended for Lambda https://github.com/aws/aws-logging-dotnet#aws-lambda

@karanrn These libraries are not a great fit for Lambda because the send the messages to CloudWatch Logs in a background thread but Lambda can freeze or recycle the compute environment before that background thread gets a chance to write the messages. I would recommend either using the ILambdaContext logging API or Amazon.Lambda.Logging.AspNetCore which are integrated with the CloudWatch Log assigned to the Lambda function and doesn’t have the background thread issue.