post-thumb

How to integrate CloudWatch with .NET Core Application in Beanstalk?

Developing and managing a web application on Beanstalk could definitely make your lives become much easier.

Because you don’t need to worry about availability, scalability, and management on your servers.

However, it’s quite tricky to integrate CloudWatch Logs in ASP.NET Core Web Application.

We still need to spend some time making AWS.Logger.AspNetCore functions properly on CloudWatch Log.

This tutorial will show you how to integrate ASP.NET Core API Logs to Amazon CloudWatch via ILogger.


Prerequisite

  • AWS.Logger.AspNetCore v3.2.0
  • Visual Studio Community v2019
  • ASP.NET Core 3.1

If you use a different version of AWS.Logger.AspNetCore and ASP.NET Core, this tutorial might not be working for you. The version of Visual Studio is not a problem. You can choose any version you want.


Step1: Install ASP.NET Core and AWS SDK

dotnet add package AWS.Logger.AspNetCore --Version 3.2.0

Step2: Create appsettings.json

{
  "Logging": {
    "Region": "<Your Region>",
    "LogGroup": "<Your Log Group>",
    "IncludeLogLevel": true,
    "IncludeCategory": true,
    "IncludeNewline": true,
    "IncludeException": true,
    "IncludeEventId": false,
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information",
      "AWS": "Debug"
    }
  },
  "AllowedHosts": "*"
}

Step3: Setup Program.cs

public class Program
{
   public static void Main(string[] args)
   {
       CreateWebHostBuilder(args).Build().Run();
   }

   public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
       WebHost.CreateDefaultBuilder(args)
            .UseIISIntegration()
           .ConfigureLogging(logging =>
           {
               logging.AddAWSProvider();
               logging.SetMinimumLevel(LogLevel.Debug);
               logging.AddDebug();    // Allowed to debug
               logging.AddConsole();  // Allowed to show logs on console

           })
           .UseStartup<Startup>();
}

Step4: Setup Startup.cs

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;

public class Startup
{
    public IConfiguration Configuration { get; }
    private ILogger<Startup> _logger;

    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        Configuration = configuration;
        this._logger = logger;
    }

    // Your Codes
    public void Configure(IApplicationBuilder app)
    {
        _logger.LogInformation("Hello from Game Tech Tutorial!");

    }

}

You can use _logger to output logs for your needs.

Note:

Remember you need to

  • set up LogGroup in CloudWatch beforehand.
  • activate Log Stream by Beanstalk Console UI on AWS.
  • create a policy below and attach to arn:aws:I am::[your AWS ID]:role/aws-elasticbeanstalk-ec2-role
{
    "Version": "2012-10-17",
    "Statement":
        {
            "Sid": "CloudWatchLogsAccess",
            "Effect": "Allow",
            "Action": "logs:*",
            "Resource": "*"
        }
    ]
}

Conclusion

In this post, you learn

  • how to install WS.Logger.AspNetCore and set up ASP.NET Core Web application and
  • how to set up Cloud Watch.
  • don’t forget to set up the retention period on CloudWatch Log which can save your money.

You might be interested in

How to implement Singed Cookies in Unity with CloudFront?

How to reverse engineer C# and Unity3D Games??