Search code examples
amazon-cloudwatch

Sending logs Cloudwatch from application running on Windows Server EC2 - log in multiple files/folders


We have an application (off the shelf, with a time-based and feature-based licence key) running on a Windows Server EC2 instance. This application creates logs which are stored in the following directory: C:\ProgramData\MyApp\v4\Logs. In this folder, there are many files with names that are {guid}.txt.

We want to feed these to Cloudwatch. We created the config.json but I wanted to ask:

Does the file_path in the config.json have to be pointing to one and only one file? Is it possible to point to a folder?

{
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "C:\\ProgramData\\MyApp\\v4\\Logs\\logs.txt",
            "log_group_name": "Logs-Myapp",
            "log_stream_name": "{instance_id}-Myapp",
            "retention_in_days": 180
          }
        ]
      }
    }
  }
}

I tried pointing to a folder but I am not getting any results.


Solution

  • The file_path inside the collect_list can only point to files. However it does allow for wildards inside the file name to select a range of files. So if your files are always named as {guid}.txt you can use the following pattern.

    {
        "logs": {
                "logs_collected": {
                        "files": {
                                "collect_list": [
                                        {
                                                "file_path": "C:\\ProgramData\\MyApp\\v4\\Logs\\*.txt",
                                                "log_group_name": "Logs-Myapp",
                                                "log_stream_name": "{instance_id}-Myapp",
                                                "retention_in_days": 180
                                        }
                                ]
                        }
                }
        }
    

    }

    The official AWS documentation page for the logging section of the cloud watch agent also describes that you can use a super asterisk (**) within file paths. In theory that could mean that you can also try the following pattern:

    "file_path": "C:\\ProgramData\\MyApp\\v4\\Logs\\**"
    

    However, I’ve never tried that and the pattern might be a bit too permissive.