From 170b53c6ee90c72835eb2dfd9665911bd633390e Mon Sep 17 00:00:00 2001 From: Bill Rawlinson Date: Mon, 23 Oct 2023 13:19:12 -0400 Subject: [PATCH] .NET v3: CloudWatch logs - fix logic so it will iterate over NextToken (#5555) --- .../DescribeLogGroups.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/dotnetv3/CloudWatchLogs/DescribeLogGroupsExample/DescribeLogGroups.cs b/dotnetv3/CloudWatchLogs/DescribeLogGroupsExample/DescribeLogGroups.cs index ffbbbb0585d..b1c1f4ede30 100644 --- a/dotnetv3/CloudWatchLogs/DescribeLogGroupsExample/DescribeLogGroups.cs +++ b/dotnetv3/CloudWatchLogs/DescribeLogGroupsExample/DescribeLogGroups.cs @@ -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); } }