gpt4 book ai didi

amazon-web-services - AWS with .NET - 从 CloudWatch 读取日志 - 没有返回日志数据

转载 作者:行者123 更新时间:2023-12-02 16:45:57 25 4
gpt4 key购买 nike

我正在尝试使用 .NET 从 CloudWatch 读取使用 SNS 发送的消息的日志数据。

从 CloudWatch 控制台 (CloudWatch\CloudWatch Logs\Logs Insights) 我输入:

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

返回大量日志记录(@timestamp | @message)

我正在尝试使用 .net AWS SDK 从 c# 执行相同的操作:

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();
}

无法弄清楚为什么它不返回任何记录。

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

知道我做错了什么吗?谢谢!

最佳答案

如果它对其他人有帮助 - 这是我使用 C# 从 AWS 获取日志的方法

public async Task GetLogs()
{
string logGroupName = "log group name from CloudWatch Log";

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 startQueryResponse = await logs.StartQueryAsync(startQueryRequest);

GetQueryResultsRequest getQueryRequest = new GetQueryResultsRequest();
getQueryRequest.QueryId = startQueryResponse.QueryId;

GetQueryResultsResponse getQueryResults = await logs.GetQueryResultsAsync(getQueryRequest);
for (var i=0; i<getQueryResults.Results.Count; i++)
{
ResultField timestampResult = getQueryResults.Results[i][0];
ResultField messageResult = getQueryResults.Results[i][1];
var message = JsonConvert.DeserializeObject(messageResult.Value);
Console.WriteLine(message);
}
Console.WriteLine();
}

关于amazon-web-services - AWS with .NET - 从 CloudWatch 读取日志 - 没有返回日志数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60483531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com