本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
CloudWatch 使用日志示例Amazon SDK for .NET
以下代码示例显示如何通过Amazon SDK for .NET和 CloudWatch 日志。
操作代码节选 CloudWatch 日志函数。
方案展示如何通过调用多个来完成特定任务的代码示例,这些示例介绍如何通过调用多个 CloudWatch 日志函数。
每个示例都包含一个指向 GitHub,其中包含了有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例显示了如何关联Amazon KMS使用现有的 CloudWatch 日志日志组。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Shows how to associate an AWS Key Management Service (AWS KMS) key with /// an Amazon CloudWatch Logs log group. The example was created using the /// AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class AssociateKmsKey { public static async Task Main() { // This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor. var client = new AmazonCloudWatchLogsClient(); string kmsKeyId = "arn:aws:kms:us-west-2:<account-number>:key/7c9eccc2-38cb-4c4f-9db3-766ee8dd3ad4"; string groupName = "cloudwatchlogs-example-loggroup"; var request = new AssociateKmsKeyRequest { KmsKeyId = kmsKeyId, LogGroupName = groupName, }; var response = await client.AssociateKmsKeyAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully associated KMS key ID: {kmsKeyId} with log group: {groupName}."); } else { Console.WriteLine("Could not make the association between: {kmsKeyId} and {groupName}."); } } }
-
有关详细信息,请参阅。AssociateKmsKey在Amazon SDK for .NETAPI 参考.
-
以下代码示例演示了如何取消现有 CloudWatch 日志导出任务。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Shows how to cancel an Amazon CloudWatch Logs export task. The example /// uses the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class CancelExportTask { public static async Task Main() { // This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor. var client = new AmazonCloudWatchLogsClient(); string taskId = "exampleTaskId"; var request = new CancelExportTaskRequest { TaskId = taskId, }; var response = await client.CancelExportTaskAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"{taskId} successfully canceled."); } else { Console.WriteLine($"{taskId} could not be canceled."); } } }
-
有关详细信息,请参阅。CancelExportTask在Amazon SDK for .NETAPI 参考.
-
以下代码示例显示了如何创建新的 CloudWatch 日志日志组。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Shows how to create an Amazon CloudWatch Logs log group. The example /// was created using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class CreateLogGroup { public static async Task Main() { // This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor. var client = new AmazonCloudWatchLogsClient(); string logGroupName = "cloudwatchlogs-example-loggroup"; var request = new CreateLogGroupRequest { LogGroupName = logGroupName, }; var response = await client.CreateLogGroupAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully create log group with ID: {logGroupName}."); } else { Console.WriteLine("Could not create log group."); } } }
-
有关详细信息,请参阅。CreateLogGroup在Amazon SDK for .NETAPI 参考.
-
以下代码示例显示了如何创建新的 CloudWatch 日志日志流。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Shows how to create an Amazon CloudWatch Logs stream for a CloudWatch /// log group. The example was created using the AWS SDK for .NET version /// 3.7 and .NET Core 5.0. /// </summary> public class CreateLogStream { public static async Task Main() { // This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor. var client = new AmazonCloudWatchLogsClient(); string logGroupName = "cloudwatchlogs-example-loggroup"; string logStreamName = "cloudwatchlogs-example-logstream"; var request = new CreateLogStreamRequest { LogGroupName = logGroupName, LogStreamName = logStreamName, }; var response = await client.CreateLogStreamAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"{logStreamName} successfully created for {logGroupName}."); } else { Console.WriteLine("Could not create stream."); } } }
-
有关详细信息,请参阅。CreateLogStream在Amazon SDK for .NETAPI 参考.
-
以下代码示例演示了如何删除 CloudWatch 日志日志组。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Uses the Amazon CloudWatch Logs Service to delete an existing /// CloudWatch Logs log group. The example was created using the /// AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DeleteLogGroup { public static async Task Main() { var client = new AmazonCloudWatchLogsClient(); string logGroupName = "cloudwatchlogs-example-loggroup"; var request = new DeleteLogGroupRequest { LogGroupName = logGroupName, }; var response = await client.DeleteLogGroupAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully deleted CloudWatch log group, {logGroupName}."); } } }
-
有关详细信息,请参阅。DeleteLogGroup在Amazon SDK for .NETAPI 参考.
-
以下代码示例演示了如何描述 CloudWatch 日志导出导出任务。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Shows how to retrieve a list of information about Amazon CloudWatch /// Logs export tasks. The example was created using the AWS SDK for .NET /// version 3.7 and .NET Core 5.0. /// </summary> public class DescribeExportTasks { public static async Task Main() { // This client object will be associated with the same AWS Region // as the default user on this system. If you need to use a // different AWS Region, pass it as a parameter to the client // constructor. var client = new AmazonCloudWatchLogsClient(); var request = new DescribeExportTasksRequest { Limit = 5, }; var response = new DescribeExportTasksResponse(); do { response = await client.DescribeExportTasksAsync(request); response.ExportTasks.ForEach(t => { Console.WriteLine($"{t.TaskName} with ID: {t.TaskId} has status: {t.Status}"); }); } while (response.NextToken is not null); } }
-
有关详细信息,请参阅。DescribeExportTasks在Amazon SDK for .NETAPI 参考.
-
以下代码示例演示了如何描述 CloudWatch 记录日志日志日志组。
- Amazon SDK for .NET
-
提示 要了解如何设置和运行此示例,请参阅GitHub
. using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Retrieves information about existing Amazon CloudWatch Logs log groups /// and displays the information on the console. The example was created /// using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DescribeLogGroups { public static async Task Main() { // Creates a CloudWatch Logs client using the default // user. If you need to work with resources in another // AWS Region than the one defined for the default user, // pass the AWS Region as a parameter to the client constructor. var client = new AmazonCloudWatchLogsClient(); var request = new DescribeLogGroupsRequest { Limit = 5, }; var response = await client.DescribeLogGroupsAsync(request); if (response.LogGroups.Count > 0) { do { 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"); }); } while (response.NextToken is not null); } } }
-
有关详细信息,请参阅。DescribeLogGroups在Amazon SDK for .NETAPI 参考.
-