Exemplo n.º 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"AppInsightsAzureFunctionDemo Start: {DateTime.Now}");

            var timer = System.Diagnostics.Stopwatch.StartNew();

            // Add custom data to the event
            // customEvents | where timestamp > ago(7d) | order by timestamp desc
            // https://microsoft.github.io/AzureTipsAndTricks/blog/tip195.html
            var dictionary = new Dictionary <string, string>();

            dictionary.Add("Function", "My AppInsightsAzureFunctionDemo");
            dictionary.Add("Caller", "Toni Pohl");
            dictionary.Add("Description", "This is the Azure function using App Insights description");
            _provider.TrackEvent($"Event: Start {DateTime.Now}", dictionary);

            // track a numeric value
            // customMetrics | order by timestamp desc
            _provider.TrackMetric("Metric: Ticks based on current time", DateTime.Now.Ticks);

            // track an exception
            // exceptions | order by timestamp desc
            _provider.TrackException(new Exception($"Exception: at {DateTime.Now}"));

            // track long running external service call
            // dependencies | order by timestamp desc
            _provider.TrackDependency("myDependency", "MyType", "myCall 1", DateTime.Now, timer.Elapsed, true);

            // Done
            _provider.TrackEvent($"Event: End {DateTime.Now}");

            string responseMessage = $"AppInsightsAzureFunctionDemo End: {DateTime.Now}";

            log.LogInformation(responseMessage);

            return(new OkObjectResult(responseMessage));
        }