DEV Community

Cover image for Creating AWS CloudWatch Logs using .NET Console Application: A Step-by-Step Guide
Dileepa Mabulage
Dileepa Mabulage

Posted on

Creating AWS CloudWatch Logs using .NET Console Application: A Step-by-Step Guide

Welcome to a comprehensive guide on creating AWS CloudWatch Logs using a .NET Console Application. In this article, we will go through the process of setting up CloudWatch and logging events from a .NET Console Application. Whether you are new to AWS or a seasoned user, this guide will provide you with a clear understanding of how to monitor and manage your application logs in the AWS Cloud. Let’s get started!

Table of Contents

  1. Create IAM Client in AWS

  2. Generate IAM Client Credentials

  3. Configure AWS CloudWatch

  4. Create .Net Console Application

  5. Add appsettings.json file

  6. Access values inside appsettings.json file

  7. Configure AWS Cloudwatch in Console App

  8. CloudWatch Log Stream

1. Create IAM Client in AWS

Identity and Access Management (IAM) is a web service for securely controlling access to Amazon Web Services services. With IAM, you can centrally manage users, security credentials such as access keys, and permissions that control which Amazon Web Services resources users and applications can access.

  1. Login to amazon with credentials

  2. From home select your preferred region

AWS Console Home Page

Region selection list

An AWS region is a cluster of data centers in a specific geographic area, such as the Northeastern United States or Western Europe. It is advisable to choose a region that is geographically close to users. This reduces latency because the data reaches the users more quickly.

  1. Go to IAM Services page

IAM Page

  1. Select Users from the left Access management panel

  2. Click Add Users

  3. Enter username => Next

  4. Select Attach Policies Directly in Permission Options

  5. Select CloudWatchFullAccess from Permission Policies

Set Permissions to IAM Client

IAM client Creation

  1. Next => Create User

Created client

  1. Now you can see the created IAM User

2. Generate IAM Client Credentials

  1. Click on the created IAM user

  2. Navigate to security credentials

Generate keys for client

  1. Scroll down to Access Keys

Generate keys for client

  1. Create access key => select other => Next

Configure access key permission

  1. Create an access key

  2. Copy both the access key and the Secret access key for later use

Access key and secret key

  1. Done

3. Configure AWS CloudWatch

CloudWatch Logs enable you to centralize the logs from all of your systems, applications, and AWS services that you use, in a single, highly scalable service. You can then easily view them, search them for specific error codes or patterns, filter them based on specific fields, or archive them securely for future analysis.

  1. Navigate CloudWatch in AWS Services

  2. Go to Log groups in the left panel (Make sure you have selected the correct AWS region)

AWS CloudWatch logGroups

  1. Click on create Log group

    A CloudWatch Log Group is a container for log streams, which are collections of log events that share the same source. For example, you could create a log group for logs generated by an application, and then create separate log streams for each instance of the application.

  2. Enter a suitable name and set the expiry date

Log group Creation

  1. Create

Successfully created logGroup

  1. Go to Log Group

  2. Create a Log Stream

    A CloudWatch Log Stream is a sequence of log events that share the same source. Within a log group, each log stream has a unique name and stores events independently. You can view and search the log events in a stream, set up alarms to be triggered by specific patterns in the log data, and export the log data to other services for further analysis.

LogStream Creation

Enter log Stream Name => create

4. Create .Net Console Application

  1. Open Visual Studio

  2. Create a new project => Select the console app

Select Console Application of Visual Studio

  1. Next => Enter Project name => Next => .Net 6.0 => Create

Sample code in Console App

5. Add appsettings.json file

The “appsettings.json” file in .NET projects is used to store configuration settings for an application. It allows you to store key-value pairs that represent various configuration options such as database connection strings, API keys, and other application-specific settings.

  1. Right-click on testAwsConsoleApp => Add => New item

Add appsettings.json file

  1. Search json in the search box

File type selection

  1. Select json file and set the name to appsessings.json => Add

  2. Add this code to appsettings.json file

    {
      "Client_id": "<your client Id>",
      "Client_secret": "<Your client secret>",
      "LogGroupName": "<Log group name>",
      "LogStreamName": "<Log stream name>",
    }
Enter fullscreen mode Exit fullscreen mode

Code on appsettings.json file

  1. Click appsettings.json file on the solution explorer and set its Copy to Output Directory value to Copy always

appsettings.json file configuration

6. Access values inside appsettings.json file

  1. Go to Program.cs file

  2. Remove all the sample code

  3. Paste this code

    using Microsoft.Extensions.Configuration;

    namespace testAwsConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                IConfigurationBuilder configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                IConfiguration configuration = configBuilder.Build();
                Console.WriteLine(configuration["Client_id"]);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode
  1. Install these Nuget packages from Nuget package manager

Nuget package manager

  1. Run the program Ctrl + F5 or green triangle on the top

  1. Now we can access the appsettings.json values from our code

7. Configure AWS Cloudwatch in Console App

  1. Create a new c# class AWSCloudWatch.cs

Add new c# class

  1. Select the class and set the name to AWSCloudWatch.cs

  2. Add

AWSCloudWatch.cs file

  1. Install this Nuget package

AWSSDK Nuget package

  1. Paste this code in AWSCloudWatch.cs > In RegionEndpoint make sure you select the accurate region in which you create the log group and log stream
    using Amazon.CloudWatchLogs;
    using Amazon.CloudWatchLogs.Model;
    using Microsoft.Extensions.Configuration;

    namespace testAwsConsoleApp
    {
        public class AWSCloudWatch
        {
            private readonly IConfiguration _configuration;
            private static System.Timers.Timer aTimer;

            public AWSCloudWatch(IConfiguration configuration)
            {
                _configuration = configuration;
            }

            public void TimerStart()
            {
                Console.WriteLine("\nPress the Enter key to exit the application...\n");
                Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                aTimer = new System.Timers.Timer(4000); // method executes every 4 seconds
                aTimer.Elapsed += (s, e) => CloudWatchLog();
                aTimer.AutoReset = true;
                aTimer.Enabled = true;
                Console.ReadLine();
                aTimer.Stop();
                aTimer.Dispose();
                Console.WriteLine("Terminating the application...");

                while (Console.Read() != 'q') ;

            }

            public async void CloudWatchLog()
            {
                try
                {
                    var credentials = new Amazon.Runtime.BasicAWSCredentials(_configuration["Client_id"], _configuration["Client_secret"]);

                    var config = new AmazonCloudWatchLogsConfig
                    {
                        RegionEndpoint = Amazon.RegionEndpoint.APSoutheast1
                    };

                    var logClient = new AmazonCloudWatchLogsClient(credentials, config);

                    await logClient.PutLogEventsAsync(new PutLogEventsRequest()
                    {
                        LogGroupName = _configuration["LogGroupName"],
                        LogStreamName = _configuration["LogStreamName"],
                        LogEvents = new List<InputLogEvent>()
                        {
                            new InputLogEvent()
                            {
                                Message = "error message from console app",
                                Timestamp = DateTime.UtcNow
                            }
                        }

                    });
                    Console.WriteLine("Logging successfull");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message, "Error occured");
                }

            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

Here we created a timer for cloud watch log execution and set the log as error message from console app . The timer calls the CloudWatchLog function every 4 seconds.

And you can set up custom error messages as well as API call error messages as InputLogEvent message. So you can log the API error messages to AWS cloudwatch.

  1. Complete Program.cs code
    using Microsoft.Extensions.Configuration;

    namespace testAwsConsoleApp
    {
        class Program
        {        
            static void Main(string[] args)
            {
                IConfigurationBuilder configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                IConfiguration configuration = configBuilder.Build();

                var cloudwatchParam = new AWSCloudWatch(configuration);

                cloudwatchParam.TimerStart();
            }

        }
    }
Enter fullscreen mode Exit fullscreen mode
  1. Run the program

Console Application

8. CloudWatch Log Stream

  1. Navigate to your cloud watch log stream

Inside log stream

These are all the logs that we send from .Net console application

Source Code

Github

Thank you….

Top comments (0)