Getting Metrics from Amazon CloudWatch - AWS SDK for .NET (version 3)

This documentation is for version 3.0 of the AWS SDK for .NET, which is mostly centered around .NET Framework and ASP.NET 4.x, Windows, and Visual Studio.

The latest version of the documentation at is mostly centered around .NET Core and ASP.NET Core. In addition to Windows and Visual Studio, it gives equal consideration to cross-platform development.

Getting Metrics from Amazon CloudWatch

This example shows you how to:

  • Retrieve a list of CloudWatch metrics

  • Publish CloudWatch custom metrics

The Scenario

Metrics are data about the performance of your systems. You can enable detailed monitoring of some resources such as your Amazon EC2 instances or your own application metrics. In this example, you use .NET to retrieve a list of published CloudWatch metrics and publish data points to CloudWatch metrics using these methods of the AmazonCloudWatchClient class:

For more information about CloudWatch metrics, see Using Amazon CloudWatch Metrics in the Amazon CloudWatch User Guide.

Prerequisite Tasks

To set up and run this example, you must first:

List Metrics

Create a ListMetricsRequest object containing the parameters needed to list metrics within the AWS/Logs namespace. Call the ListMetrics method from a AmazonCloudWatchClient instance to list the IncomingLogEvents metric.

var logGroupName = "LogGroupName"; DimensionFilter dimensionFilter = new DimensionFilter() { Name = logGroupName }; var dimensionFilterList = new List<DimensionFilter>(); dimensionFilterList.Add(dimensionFilter); var dimension = new Dimension { Name = "UniquePages", Value = "URLs" }; using (var cw = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { var listMetricsResponse = cw.ListMetrics(new ListMetricsRequest { Dimensions = dimensionFilterList, MetricName = "IncomingLogEvents", Namespace = "AWS/Logs" }); Console.WriteLine(listMetricsResponse.Metrics); }

Submit Custom Metrics

Create a PutMetricDataRequest object containing the parameters needed to submit a data point for the PAGES_VISITED custom metric. Call the PutMetricData method from the AmazonCloudWatchClient instance.

using (var cw = new AmazonCloudWatchClient(RegionEndpoint.USWest2)) { cw.PutMetricData(new PutMetricDataRequest { MetricData = new List<MetricDatum>{new MetricDatum { MetricName = "PagesVisited", Dimensions = new List<Dimension>{dimension}, Unit = "None", Value = 1.0 }}, Namespace = "SITE/TRAFFIC" }); }