Reputation: 91
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
Upvotes: 3
Views: 6767
Reputation: 1391
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 );
}
}
}
Upvotes: 9