Game Tech Tutorial
Published in

Game Tech Tutorial

How to integrate ASP.NET Core Web to CloudWatch on AWS Beanstalk?

AWS Elastic Beanstalk is a managed service that allows you can ensure high availability, scalability, and less administration of your servers.

If you develop an ASP.NET web application on AWS Beanstalk, this can definitely make your lives become much easier.

You can simply deploy your web application to AWS Beanstalk by default settings which will automatically create 3-tier architecture including application load balancer, web application, and database.

Furthermore, you can even customize the settings of VPC, Database, Application Load Balancer, Monitoring, Logs, and CloudWatch Metrics for scalability when needed.

However, it’s quite tricky to integrate CloudWatch Logs with ASP.NET Core Web Application on AWS Beanstalk.

Given the fact that AWS Beanstalk, by default, does not support dumping logs for Windows Server while Linux is fully supported on CloudWatch.

You need to spend some time tweaking the ASP.NET Core settings and make it work properly on CloudWatch Log.

This tutorial will help you to smoothly integrate ASP.NET Core API Logs to Amazon CloudWatch via ILogger on AWS Beanstalk.

Getting Started

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 number of Visual Studio does not make any difference. 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 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 have learned

  • how to install WS.Logger.AspNetCore and set up ASP.NET Core Web application and
  • how to set up Cloud Watch on AWS Beanstalk.
  • Remember you need to set up the IAM policy properly to allow AWS Beanstalk to access CloudWatch Logs.

Don’t forget to set up the retention period in CloudWatch Log which can save your money.

I hope this tutorial would be helpful for you.

Thanks for reading.

You might be interested in

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eric Wei

Eric Wei

Senior Full Stack Engineer & Solution architecture | AWS, GCP, Azure | Cloud, Unity Game Development, SDK, DevOps, and more.