Michael Harris
Michael Harris

Reputation: 265

Why can't I log anything from a .NET Core app running on AWS ECS?

I have an app written in .NET Core 3.1, dockerized, and deployed to AWS ECS. My Task Definition is configured to send logs to CloudWatch.

No matter what I do, I can't seem to configure logging within my app to send custom logs.

Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(o =>
                {
                    o.AddAWSProvider();
                    o.SetMinimumLevel(LogLevel.Debug);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

MyService.cs

[ServiceContract]
    public class HealthCheckService
    {
        private readonly ILogger<HealthCheckService> _logger;

        public HealthCheckService(ILogger<HealthCheckService> logger)
        {
            _logger = logger;
        }

        [OperationContract]
        public string HealthCheck(string input)
        {
            _logger.LogInformation("test from health check");
            return input;
        }
    }

The service endpoint works. I can call it and get back a good response. But in CloudWatch I don't see anything but the same few basic logs.

Shouldn't logging be stupidly simple? Perhaps it is, and I should just feel very poorly about myself...

Upvotes: 0

Views: 1651

Answers (1)

  1. Check if your Task has sufficient permissions for writing to CloudWatch logs. The role that manages your permissions is the Task Role (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html) Task role

I am testing with CloudWatchFullAccess policy Permissions

  1. Check if you are looking in the correct region. My configuration is like this(https://github.com/aws/aws-logging-dotnet#aspnet-core-logging)

     "Logging": {
         "IncludeCategory": true,
         "IncludeEventId": false,
         "IncludeException": true,
         "IncludeLogLevel": true,
         "IncludeNewline": true,
         "IncludeScopes": false,
         "LogGroup": "AspNetCore.WebSample",
         "LogLevel": {
           "Default": "Debug",
           "System": "Information",
           "Microsoft": "Information"
         },
        "Region": "eu-central-1"
    }
    

    My CloudWatch logs are in Europe (Frankfurt) eu-central-1

Nothing is as easy as it should be, so don't worry if things don't work out the first time.

Upvotes: 2

Related Questions