Skip to content

Commit

Permalink
.NET v3: CloudWatch logs - fix logic so it will iterate over NextToken (
Browse files Browse the repository at this point in the history
  • Loading branch information
finalcut authored Oct 23, 2023
1 parent 79477b8 commit 170b53c
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,42 @@ public static async Task Main()
// pass the AWS Region as a parameter to the client constructor.
var client = new AmazonCloudWatchLogsClient();

bool done = false;
string? newToken = null;

var request = new DescribeLogGroupsRequest
{
Limit = 5,
};

var response = await client.DescribeLogGroupsAsync(request);
DescribeLogGroupsResponse response;

if (response.LogGroups.Count > 0)
do
{
do
if (newToken is not null)
{
request.NextToken = newToken;
}

response = await client.DescribeLogGroupsAsync(request);

response.LogGroups.ForEach(lg =>
{
Console.WriteLine($"{lg.LogGroupName} is associated with the key: {lg.KmsKeyId}.");
Console.WriteLine($"Created on: {lg.CreationTime.Date.Date}");
Console.WriteLine($"Date for this group will be stored for: {lg.RetentionInDays} days.\n");
});

if (response.NextToken is null)
{
done = true;
}
else
{
response.LogGroups.ForEach(lg =>
{
Console.WriteLine($"{lg.LogGroupName} is associated with the key: {lg.KmsKeyId}.");
Console.WriteLine($"Created on: {lg.CreationTime.Date.Date}");
Console.WriteLine($"Date for this group will be stored for: {lg.RetentionInDays} days.\n");
});
newToken = response.NextToken;
}
while (response.NextToken is not null);
}
while (!done);
}
}

Expand Down

0 comments on commit 170b53c

Please sign in to comment.