I am using C# AWS SDK to retieve CloudWatch logs in (date-time) descending order (latest-first). So setting up the AmazonCloudWatchLogsClient
as follows:
AmazonCloudWatchLogsConfig config = new AmazonCloudWatchLogsConfig {
RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName("<aws-region>")
};
AmazonCloudWatchLogsClient _client = new AmazonCloudWatchLogsClient(config);
_client.DescribeLogStreamsAsync(new DescribeLogStreamsRequest {
LogGroupName = "logGroup",
OrderBy = "LastEventTime",
Descending = true,
});
And setting up cloud watch group and stream as follows:
_client.CreateLogGroupAsync(new CreateLogGroupRequest { LogGroupName = "logGroup" });
_client.CreateLogStreamAsync(new CreateLogStreamRequest {
LogGroupName = "logGroup",
LogStreamName = "logStream"
});
Than retrieving cloud watch logs as follows:
var request = new GetLogEventsRequest
{
LogStreamName = "logStream",
LogGroupName = "logGroup",
StartTime = DateTime.UtcNow.AddDays(-1),
EndTime = DateTime.UtcNow,
Limit = 50,
StartFromHead = false
};
var response = await GetLogEventsAsync(123, 456, 50, "000/f", "000/b");
var logs = response.Events;
The logs
are received as always oldest-first, not the latest-first.
Please suggest any fixes.
Shakeel, I wondered if you had tried the extension method OrderByDescending? I was able to get this to work by using that:
returnValue.AddRange(logEventsResponse.Events.OrderByDescending(y => y.Timestamp).Select(x => new CloudWatchLogEvent { Message = x.Message, Timestamp = x.Timestamp.AddHours(-5), StreamName = stream.LogStreamName }));
returnValue is type List<CloudWatchLogEvent>
and logEventsResponse is Task<GetLogEventsResponse>
and CloudWatchLogEvent is a simple class I created:
public class CloudWatchLogEvent
{
public string Message { get; set; }
public DateTime Timestamp { get; set; }
public string StreamName { get; set; }
}