Spent lot of time in this. But, I am not getting a way to query logs from cloud watch to c# api. I want to display those logs on UI. Any help? Thanks in advance
Asked
Active
Viewed 5,066 times
2
-
1you can use aws sdk to get logs from cloudwatch AmazonCloudWatchLogsClient – xoxo Jul 01 '20 at 05:19
-
You could use AWSSDK.Core for dotnet core. This [solution](https://stackoverflow.com/a/60518796/7255591) helped me – Akshay Jul 22 '21 at 08:46
1 Answers
7
You can integrate with cloudwatch and access for logs by using below code. First and foremost you have to install awssdk with latest version and you have to provide aws accessKey and secretKey, your regionEndpoint along with loggroup name of cloudwatch.
public static void DescribeSubscriptionFilters()
{
var credentials = new BasicAWSCredentials("awskey", "secretkey"); // provide aws credentials
IAmazonCloudWatchLogs client =
new AmazonCloudWatchLogsClient(credentials, RegionEndpoint.USGovCloudWest1); // provide regionEndPoint
var describeLogStreamsRequest = new DescribeLogStreamsRequest()
{
LogGroupName = "LogGroupName" //mention your cloudwatch log group
};
var describeLogStreamsResult = client.DescribeLogStreams(describeLogStreamsRequest);
foreach (var stream in describeLogStreamsResult.LogStreams)
{
var eventsRequest = new GetLogEventsRequest()
{
LogStreamName = stream.LogStreamName,
LogGroupName = describeLogStreamsRequest.LogGroupName
};
var result = client.GetLogEvents(eventsRequest);
foreach (var events in result.Events)
{
Console.WriteLine(events.Timestamp + " - " + events.Message );
}
}
}

xoxo
- 1,248
- 14
- 19