static double GetMetricValue(SubscriptionCloudCredentials cred,
            MetricsClient metricsClient,
            string webSiteResourceId,
            string metricName)
        {
            double requestCount = 0;

            var metricValueResult = metricsClient.MetricValues.List(
                                    webSiteResourceId,
                                    new List<string> { metricName },
                                    "",
                                    TimeSpan.FromHours(1),
                                    DateTime.UtcNow - TimeSpan.FromDays(1),
                                    DateTime.UtcNow
                                    );

            var values = metricValueResult.MetricValueSetCollection;

            foreach (var value in values.Value)
            {
                foreach (var total in value.MetricValues)
                {
                    if (total.Total.HasValue)
                        requestCount += total.Total.Value;
                }
            }

            return requestCount;
        }
예제 #2
0
        protected override void Clone(ServiceClient <MetricsClient> client)
        {
            base.Clone(client);

            MetricsClient metricsClient = client as MetricsClient;

            if (metricsClient != null)
            {
                metricsClient._credentials = Credentials;
                metricsClient._baseUri     = BaseUri;
                metricsClient.Credentials.InitializeServiceClient(metricsClient);
            }
        }
        private static void ShowWebSiteMetrics(SubscriptionCloudCredentials cred)
        {
            using (var webSitesMgmtClient = new WebSiteManagementClient(cred))
            {
                using (var metricsClient = new MetricsClient(cred))
                {
                    var webSiteResourceIds = GetWebSiteResourceIds(cred, webSitesMgmtClient);

                    webSiteResourceIds.ForEach(webSiteResourceId =>
                    {
                        Console.WriteLine(webSiteResourceId);

                        var metricDefinitions = ListResourceMetrics(cred, metricsClient, webSiteResourceId);

                        metricDefinitions.OrderBy(x => x.DisplayName).ToList().ForEach(metric =>
                        {
                            var metricValue = GetMetricValue(
                                cred,
                                metricsClient,
                                webSiteResourceId,
                                metric.Name);

                            Console.WriteLine("{0} ({1}): {2} ({3})",
                                metric.DisplayName,
                                metric.Name,
                                ((metric.Unit == "Bytes")
                                    ? metricValue.BytesToString()
                                    : (metric.Unit == "Milliseconds")
                                        ? metricValue.MillisecondsToHours()
                                        : metricValue.ToString()),
                                metric.Unit);
                        });
                    });
                }
            }
        }
        static List<MetricDefinition> ListResourceMetrics(SubscriptionCloudCredentials cred,
            MetricsClient metricsClient,
            string webSiteResourceId)
        {
            var metricListResponse = metricsClient.MetricDefinitions.List(webSiteResourceId,
                null, // don't filter the list of metric definitions available
                null  // no need to pass anything here
                );

            var metricCollection = metricListResponse.MetricDefinitionCollection.Value;
            return metricCollection.ToList();
        }