Search code examples
amazon-web-services.net-coreamazon-cloudwatchlogsaws-cloudwatch-log-insights

AWS with .NET - reading logs from CloudWatch - no log data returned


I am attempting to read Log data using .NET from CloudWatch for Messages sent using SNS.

From the CloudWatch console (CloudWatch \ CloudWatch Logs \ Logs Insights) I enter:

Date range: custom (2w)
LogGroup: sns/ap...../8...../LogName 
Query: fields @timestamp, @message | sort @timestamp desc | limit 20

It returns lots of log records (@timestamp | @message)

I'm trying to do the same from c# using the .net AWS SDK:

public async Task GetLogs()
{
    string logGroupName = "sns/ap...../8...../LogName";

    AWSOptions options = configuration.GetAWSOptions();
    IAmazonCloudWatchLogs logs = options.CreateServiceClient<IAmazonCloudWatchLogs>();

    StartQueryRequest startQueryRequest = new StartQueryRequest();
    startQueryRequest.LogGroupName = logGroupName;
    startQueryRequest.StartTime = 1577850562; //1 Jan 2020
    TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); //Epoch time starts on 1/1/1970
    int secondsSinceEpoch = (int)t.TotalSeconds;
    startQueryRequest.EndTime = secondsSinceEpoch;
    startQueryRequest.QueryString = "fields @timestamp, @message | sort @timestamp desc";
    startQueryRequest.Limit = 1000;
    StartQueryResponse response2 = await logs.StartQueryAsync(startQueryRequest);

    Console.WriteLine();
}

Can't work out why it doesn't return any records.

Response2 = 
   ContentLength: 50
   QueryId: "guid..."
   ResponseMetadata:
      Metadata Count = 0
      RequestId = "guid..."

Any idea what I'm doing wrong? Thanks!


Solution

  • You are only starting the query, you need to call GetQueryResults to get the data: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_GetQueryResults.html