简体   繁体   English

Elastic Beanstalk output 自定义日志到

[英]Elastic Beanstalk output custom logs to

I have an Elastic Beanstalk app running a.Net app through IIS in a Windows environment.我有一个 Elastic Beanstalk 应用程序在 Windows 环境中通过 IIS 运行 a.Net 应用程序。 I want to get my custom logs to show up in AWS CloudWatch.我想让我的自定义日志显示在 AWS CloudWatch 中。

The app uses a Serilog logger in one of its static classes.该应用程序在其 static 类之一中使用了 Serilog 记录器。
The logger outputs a message when I go to an endpoint path (ex. "/api/log-a-message").当我 go 到端点路径(例如“/api/log-a-message”)时,记录器会输出一条消息。
Logs are written to a file "C:\LogsFolder\LogFile.log".日志被写入文件“C:\LogsFolder\LogFile.log”。

Following some online searching and reading through other questions and the AWS Documents.进行一些在线搜索并阅读其他问题和 AWS 文档。 I ended up writing a .ebextensions/log_configuration.conf with the following content:我最终写了一个.ebextensions/log_configuration.conf包含以下内容:

### BEGIN .ebextensions/CloudWatch.config
files:
  "C:/Program Files/Amazon/ElasticBeanstalk/config/taillogs.d/":
    content: |
      [ZeW logs]
      log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "Serilog"]]}`
      log_stream_name = {instance_id}
      file = C:/LogsFolder/LogFile.log
### END .ebextensions/CloudWatch.config

But the logs are still not appearing in CloudWatch.但日志仍未出现在 CloudWatch 中。

I've managed to almost do it.我几乎做到了。 ... So it turns out that for AWS CloudWatch you need to append a configuration in JSON format. ...所以事实证明,对于 AWS CloudWatch,您需要 append JSON 格式的配置。

Below is my .ebextensions/custom_logs.config file that Elastic Beanstalk uses for extensions.下面是 Elastic Beanstalk 用于扩展的我的.ebextensions/custom_logs.config文件。 This just creates a custom_logs.json file for CloudWatch to use.这只是创建一个custom_logs.json文件供 CloudWatch 使用。

files:
  "C:/Users/Administrator/Desktop/custom_logs.json":
    content: |
      {
        "agent": {
          "metrics_collection_interval": 5
        },
        "logs": {
          "logs_collected": {
            "files": {
              "collect_list": [{
                "file_path": "C:\\MyCustomLogsFolder\\MyCustomLogFile.log",
                "log_group_name": "/aws/elasticbeanstalk/UsuallyThisIsTheEnvironmentName/MyCustomLogGroup-Log",
                "timezone": "UTC",
                "timestamp_format": "%Y-%m-%d %H:%M:%S",
                "multi_line_start_pattern": "{timestamp_format}"
              }]
            }
          }
        }
      }

After that file is created on my Desktop by Elastic Beanstalk, I can connect to the instance and run the following command (including the & at the start):在 Elastic Beanstalk 在我的桌面上创建该文件后,我可以连接到实例并运行以下命令(包括开头的& ):

& C:\\'Program Files'\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1 -a append-config -m ec2 -c file:C:\\Users\\Administrator\\Desktop\\cu
stom_logs.json -s

The only thing I need to figure out now is how to do this automatically once the instance starts.我现在唯一需要弄清楚的是如何在实例启动后自动执行此操作。


Managed to do it with the following code (in addition to the files: statement above):设法使用以下代码(除了上面的files:声明)来做到这一点:

services:
  windows:
    AmazonCloudWatchAgent:
      enabled: 'true'
      ensureRunning: 'true'
      files:
        - "C:/MyCustomLogsFolder/MyCustomLogFile.log"
container_commands:
  01_cloudwatch_append:
    command: powershell.exe -ExecutionPolicy Bypass -Command "$cwa_ctl='C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1'; $custom_logs_config='C:\\Users\\Administrator\\Desktop\\custom_logs.json'; & $cwa_ctl -a append-config -m ec2 -c file:$custom_logs_config -s;"
    ignoreErrors: true
    waitAfterCompletion: 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM