Welcome to this 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!
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.
-
Login to amazon with credentials
-
From home select your preferred region
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 a best practice to choose a region that is geographically close to users. This reduces latency because the data reaches the users more quickly.
- Go to IAM Services page
-
Select Users from the left Access management panel
-
Click Add Users
-
Enter username => Next
-
Select Attach Policies Directly in Permission Options
-
Select CloudWatchFullAccess from Permission Policies
- Next => Create User
- Now you can see the created IAM User
-
Click on the created IAM user
-
Navigate to security credentials
- Scroll down to Access Keys
- Create access key => select other => Next
-
Create an access key
-
Copy both the access key and the Secret access key for later use
- Done
CloudWatch Logs enables 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.
-
Navigate CloudWatch in AWS Services
-
Goto Log groups in the left panel (Make sure you have selected the correct AWS region)
- 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.
- Enter a suitable name and set the expiry date
- Create
-
Go to Log Group
-
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.
Enter log Stream Name => create
-
Open Visual Studio
-
Create a new project => Select the console app
- Next => Enter Project name => Next => .Net 6.0 => Create
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.
- Right-click on testAwsConsoleApp => Add => New item
- Search json in the search box
-
Select json file and set the name to appsessings.json => Add
-
Add this code to appsettings.json file
{ "Client_id": "", "Client_secret": "", "LogGroupName": "", "LogStreamName": "", }
- Click appsettings.json file on the solution explorer and set its Copy to Output Directory value to Copy always
-
Go to Program.cs file
-
Remove all the sample code
-
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"]); } } }
-
Install these Nuget packages from Nuget package manager
- Run the program Ctrl + F5 or green triangle on the top
- Now we can access the appsettings.json values from our code
- Create a new c# class AWSCloudWatch.cs
-
Select the class and set the name to AWSCloudWatch.cs
-
Add
- Install this Nuget package
- 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");
}
}
}
}
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.
-
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(); } }
}
-
Run the program
- Navigate to your cloud watch log stream
These are the all logs that we send from .Net console application