首页 > 解决方案 > 无法使用 NLog 从托管在 Windows Elastic Beanstalk 机器上的 ASP.Net 应用程序将日志写入 AWS cloudwatch

问题描述

我创建了一个新的 ASP.Net 应用程序并使用其部署工具包部署在 AWS Elastic Beanstalk 机器上。现在我正在尝试向应用程序添加一个日志框架。我正在使用 NLog 并希望将日志写入 AWS CloudWatch 文件夹。

我已经从 NuGet 安装了 AWS.Logger.NLog 包,并使用如下示例 Logger 类将 NLog.config 文件添加到我的应用程序中。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true">
  <targets>
    <target name="aws" type="AWSTarget" logGroup="NLog" region="us-east-1"/>
    <target name="logfile" xsi:type="Console" layout="${callsite} ${message}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile,aws" />
  </rules>
</nlog>

public class Logger
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        public static void LogErrorMessage(String message)
        {
            logger.Error(message);
        }

        public static void LogInfoMessage(String message)
        {
            logger.Info(message);
        }
    }

当我在本地运行我的应用程序时,示例日志文件夹已成功创建,并且消息将记录在 CloudWatch 上。但是在 ElasticBeanstalk 上部署应用程序后,CW 上没有任何记录。甚至文件夹也没有被创建。

有人可以帮我解决我在这里想念的东西。

标签: asp.netamazon-web-servicesloggingnlogamazon-cloudwatchlogs

解决方案


如果包括<extensions>怎么办?

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">
  <extensions>
    <add assembly="NLog.AWS.Logger" />
  </extensions>
  <targets>
    <target name="aws" type="AWSTarget" logGroup="NLog" region="us-east-1"/>
    <target name="logfile" xsi:type="Console" layout="${callsite} ${message}" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile,aws" />
  </rules>
</nlog>

推荐阅读