Lab 2: Monitor a local web API by using Application Insights

Task 1: Build a .NET Web API project

  1. From the lab computer, start Visual Studio Code.

  2. In Visual Studio Code, on the File menu, select Open Folder.

  3. In the Open Folder window, browse to $HOME\training-az204\Labs\11\Starter\Api, and then select Select Folder.

  4. In the Visual Studio Code window, activate the shortcut menu, and then select Open in Integrated Terminal.

  5. At the terminal prompt, run the following command to create a new .NET Web API application named SimpleApi in the current directory:

    dotnet new webapi --output . --name SimpleApi --framework net6.0
    
  6. Run the following command to import version 2.20.0 of Microsoft.ApplicationInsights from NuGet to the current project:

    dotnet add package Microsoft.ApplicationInsights --version 2.20.0
    

    Note: The dotnet add package command will add the Microsoft.ApplicationInsights package from NuGet. For more information, refer to Microsoft.ApplicationInsights.

  7. Run the following command to import version 2.20.0 of Microsoft.ApplicationInsights.AspNetCore from NuGet:

    dotnet add package Microsoft.ApplicationInsights.AspNetCore --version 2.20.0
    

    Note: The dotnet add package command will add the Microsoft.ApplicationInsights.AspNetCore package from NuGet. For more information, refer to Microsoft.ApplicationInsights.AspNetCore.

  8. At the terminal prompt, run the following command to import version 2.20.0 of Microsoft.ApplicationInsights.PerfCounterCollector from NuGet to the current project:

    dotnet add package Microsoft.ApplicationInsights.PerfCounterCollector --version 2.20.0
    

    Note: The dotnet add package command will add the Microsoft.ApplicationInsights.PerfCounterCollector package from NuGet. For more information, refer to Microsoft.ApplicationInsights.PerfCounterCollector.

  9. At the terminal prompt, run the following command to import version 2.4.0 of Microsoft.ApplicationInsights.Profiler.AspNetCore from NuGet to the current project:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore --version 2.4.0
    

    Note: The dotnet add package command will add the Microsoft.ApplicationInsights.Profiler.AspNetCore package from NuGet. For more information, refer to Microsoft.ApplicationInsights.Profiler.AspNetCore.

  10. At the terminal prompt, run the following command to build the .NET Web API:

    dotnet build
    

Task 2: Update app code to disable HTTPS and use Application Insights

  1. In the Visual Studio Code window, on the Explorer pane, select the Program.cs file to open the file on the editor pane.

  2. On the editor pane, locate and delete the following code in line 19:

    app.UseHttpsRedirection();
    

    Note: This line of code forces the web API to use HTTPS. For this lab, this is unnecessary.

  3. In the Program.cs file, add the following two lines starting with line 9 to enable Profiler by registering it along with Application Insights into the Service collection:

    builder.Services.AddApplicationInsightsTelemetry();
    builder.Services.AddServiceProfiler();
    
  4. Save the changes and close the Program.cs file.

  5. In the Visual Studio Code window, on the Explorer pane, select the appsettings.json file to open the file on the editor pane.

  6. Add to the appsettings.json file the following element right after the Logging element, replacing the instrumentation-key placeholder with the value of the Application Insights resource instrumentation key that you recorded earlier in this lab:

       "ApplicationInsights":
       {
          "InstrumentationKey": "instrumentation-key"
       },
    

    Note: If the section you are adding is the last element of the file, remove the trailing comma.

  7. Save the changes to the appsettings.json file and close it.

  8. At the terminal prompt, run the following command to build the .NET Web API.

    dotnet build
    

Task 3: Test an API application locally

  1. At the terminal prompt, run the following command, and then select Enter to create a certificate. Select YES on the prompted option.

    dotnet dev-certs https --trust
    
  2. At the terminal prompt, run the following command to launch the .NET Web API.

    dotnet run
    
  3. Review the output of the command and note the HTTPS URL that the site is listening on.

  4. From the taskbar, open the context menu for the Microsoft Edge icon, and then open a new browser window.

  5. In the browser window that opens, navigate to the https://localhost URL and add the /weatherforecast relative path of your web API.

    Note: The full URL is http://localhost:[port-number]/weatherforecast, where the [port-number] placeholder identifies the port number at which the web app is accessible via the HTTPS protocol.

    Note: The page should contain an output in the following format:

    [{"date":"2021-09-04T10:15:04.0969996-07:00","temperatureC":54,"temperatureF":129,"summary":"Sweltering"},{"date":"2021-09-05T10:15:04.0972401-07:00","temperatureC":44,"temperatureF":111,"summary":"Balmy"},{"date":"2021-09-06T10:15:04.0976549-07:00","temperatureC":41,"temperatureF":105,"summary":"Scorching"},{"date":"2021-09-07T10:15:04.0976613-07:00","temperatureC":-4,"temperatureF":25,"summary":"Freezing"},{"date":"2021-09-08T10:15:04.0976618-07:00","temperatureC":33,"temperatureF":91,"summary":"Balmy"}]
    
  6. Close the browser window that's displaying the page generated by http://localhost:[port-number]/weatherforecast.

  7. In Visual Studio Code, select Kill Terminal (the Recycle Bin icon) to close the terminal pane and any associated processes.

Task 4: Review metrics in Application Insights

  1. On your lab computer, switch to the Microsoft Edge browser window displaying the Azure portal.

  2. In the Azure portal, navigate back to the blade of the instrm[yourname] Application Insights resource you created previously in this lab.

  3. On the Application Insights blade, in the tiles in the center of the blade, find the displayed metrics. Specifically, find the number of server requests that have occurred and the average server response time.

    The following screenshot displays the Application Insights metrics of the local web app.

    Application Insights metrics of the local web app in the Azure portal

    Note: It can take up to five minutes to observe requests in the Application Insights metrics charts.

Review

In this exercise, you created an API app by using ASP.NET and configured it to stream application metrics to Application Insights. You then used the Application Insights dashboard to review performance details about your API.

results matching ""

    No results matching ""