花了很多时间在这上面。但是,我没有办法将日志从云观察查询到 c# api。我想在 UI 上显示这些日志。有什么帮助吗? 提前致谢
您可以使用以下代码与 cloudwatch 集成并访问日志。 首先,您必须使用最新版本安装 awssdk,并且您必须提供 aws accessKey 和 secretKey、您的区域终端节点以及 cloudwatch 的日志组名称。
public static void DescribeSubscriptionFilters()
{
var credentials = new BasicAWSCredentials("awskey", "secretkey"); // provide aws credentials
IAmazonCloudWatchLogs client =
new AmazonCloudWatchLogsClient(credentials, RegionEndpoint.USGovCloudWest1); // provide regionEndPoint
var describeLogStreamsRequest = new DescribeLogStreamsRequest()
{
LogGroupName = "LogGroupName" //mention your cloudwatch log group
};
var describeLogStreamsResult = client.DescribeLogStreams(describeLogStreamsRequest);
foreach (var stream in describeLogStreamsResult.LogStreams)
{
var eventsRequest = new GetLogEventsRequest()
{
LogStreamName = stream.LogStreamName,
LogGroupName = describeLogStreamsRequest.LogGroupName
};
var result = client.GetLogEvents(eventsRequest);
foreach (var events in result.Events)
{
Console.WriteLine(events.Timestamp + " - " + events.Message );
}
}
}