Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Lab 11: Application Insights for ASP .NET Core
Azure Application Insights provides in-depth monitoring of your web application down to the code level. You can easily monitor your web application for availability, performance, and usage. You can also quickly identify and diagnose errors in your application without waiting for a user to report them.
This article walks you through the steps of creating a sample ASP.NET Core Razor Pages application in Visual Studio. It also shows you how to start monitoring by using Application Insights.
a.
1. 2.
Create an ASP.NET application project
Estimation time for this section A: 5 Minutes
Open Visual Studio. From the main menu, select File > New > Project.
Level 3
Asia Pacific University of Technology & Innovation
Page 1 of 23
3.
In the New Project dialog box, select Web > ASP.NET Core Web
Application > {anyname}. Then select OK.
In the New ASP.NET Core Web Application dialog box, select .NET
Core > ASP.NET Core 2.0 > Web Application. Then select OK.
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
b. Test your Localhost application together with Application Insights.
Estimation time for this section B: 10 Minutes 1. Add Application Insights Telemetry.
2. Select Project > Add Application Insights Telemetry. (Or, you can right-click Connected Services, and then select Add Connected Service.) Then, click the button “Get Started”.
1. Select your subscription, then select Resource > Register.
Level 3
Asia Pacific University of Technology & Innovation Page 2 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Level 3 Asia Pacific University of Technology & Innovation Page 3 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
c. Check the changes made to your project.
Estimation time for this section C: 5 Minutes
Application Insights is low overhead. To review the modifications made to your project by adding Application Insights telemetry:
1.
In the Solution Explorer, there are some changes appear: ConnectedService.json was added
appsettings.json
Program.cs
Level 3
Asia Pacific University of Technology & Innovation
Page 4 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
d. Synthetic transactions with Windows PowerShell.
Estimation time for this section D: 10 Minutes
To automate request to your app by using synthetic transactions:
1. Run your app. To run your app, select the IIS Express icon.
2. Copy the URL from the browser address bar. The URL is in the format http://localhost:
3. Run the following PowerShell loop to create 100 synthetic transactions by using your test app. For example:
Level 3
Asia Pacific University of Technology & Innovation Page 5 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
4. To start the windows PowerShell, follow the below steps :
Level 3
Asia Pacific University of Technology & Innovation Page 6 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
e. Open the Application Insights portal.
Estimation time for this section E: 10 minutes.
After you run the PowerShell commands in the preceding section, open Application
Insights to view the transactions and to confirm that data is being collected.
1. In the Visual Studio menu, select Project > Application Insights > Open Application Insights Portal.
Level 3 Asia Pacific University of Technology & Innovation Page 7 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Level 3
Asia Pacific University of Technology & Innovation
Page 8 of 23
Notes:
In the preceding example screenshot, Live Stream, Page View Load Time, and Failed Requests aren’t collected. The next section walks you through the steps for adding each of these. If you’re already collectingLive
Stream and Page View Load Time, complete the steps only Requests.
for Failed
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
f. Collect Failed Request, Live Stream, and Page View Load Time
Estimation time for this section F: 10 Minutes. Failed Requests
Technically, failed requests are being collected, but no failed requests have occurred yet. To speed the process, you can add a custom exception to the existing project to simulate a real-world exception. If your app is still running in Visual Studio, before you proceed, select Stop Debugging (Shift+F5).
1. 2.
In Solution Explorer, expand Pages > About.cshtml, and then open and modify the file of About.cshtml.cs.
Add an exception under Message=, and then save the change to the file. Example as below:
Level 3
Asia Pacific University of Technology & Innovation Page 9 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Live Stream
To access the Live Stream functionality of Application Insights with ASP.NET Core, update the Microsoft.ApplicationInsights.AspNetCore 2.2.0 NuGet packages.
1.
In Visual Studio, select Project > Manage NuGet Packages > Microsoft.ApplicationInsights.AspNetCore > (version) 2.5.1 > Update.
Level 3
Asia Pacific University of Technology & Innovation
Page 10 of 23
2.
Multiple confirmation prompts appear. Read and accept if you agree with the
changes.
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Page View Load Time
1. In Visual Studio, go to Solution Explorer > Pages. You must modify two files: Layout.cshtml and ViewImports.cshtml.
2. In ViewImports.cshtml, add this code:
3. In Layout.cshtml, add the following code before the tag and before any other scripts:
Level 3
Asia Pacific University of Technology & Innovation Page 11 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
g.
Test Failed Request, Page View Load Time, and Live Stream.
Estimation time for this section G: 10 Minutes.
1.
To test and confirm that everything is working:
Run your app. To run your app, select the IIS Express icon.
Go to the About page to trigger the test exception. (If you’re in debug mode, in Visual Studio, selectContinuefor the exception to show up in Application Insights.)
Rerun the simulated PowerShell transaction script that you used earlier. (You might need to adjust the port number in the script.)
If the Overview page in Applications Insights still isn’t open, in the Visual Studio
menu, select Project > Application Insights > Open Application Insights Portal.
Notes:
If you don’t see your new traffic, check the value for Time range, and then select Refresh. If you would like to see the CPU usage in the live stream dashboard, kindly publish your application to the cloud with the application insights function.
Level 3
Asia Pacific University of Technology & Innovation Page 12 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
h.
Test your online application using Application Insights
Estimation time for this section H: 10 Minutes.
1. Upload your application to the Azure Account.
2. In Solution Explorer, right click on your project, and then click the publish
button. Then, make sure your application is online now.
Level 3
Asia Pacific University of Technology & Innovation Page 13 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
3.
4.
Now, click on the new application insights service that attached in your Azure Dashboard. Then, replace the localhost url in the PowerShell script with the real Azure web application’s url and execute the script. If you unable to run the script in your computer, you also can use the Azure PowerShell to run the for loop script.
Continue by repeating the same steps in Section E, F, and G.
Level 3
Asia Pacific University of Technology & Innovation Page 14 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
i. Creating A URL Ping Test
Estimation time for this section I: 15 Minutes.
A URL Ping test is created within an Application Insights resource in the Azure portal.
1.
Once your Application Insights resource is available, browse to it within the Azure portal so that you are on the Overview blade. Scroll down the blade past the Application health section until you see a group of tiles. Within this group you will see one marked Availability.
Click on the Availability tile to start creating your first web test. The Web tests blade will appear.
2.
Level 3
Asia Pacific University of Technology & Innovation Page 15 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
3. Click on the Add web test button. The Create test blade will then open. Notice that the system tried to guess which URL you wished to test and so it created an Azure Websites address using the Application Insights resource name.
4. To start configuring the web test, enter a descriptive name as the Test name. The Test type defaults to URL ping test but as you can see in the figure below the Multi-Step test is also available. Next, enter URL to test with this web test. The URL must accessible on the public internet. It can also include query strings and it will follow up to 10 redirects. For illustration purposes, enter the Microsoft.com but for your test you would enter URL of the website or web application you wish to test.
5. Next, click the Enable retries for the web test failures check box if you want the test to retry the URL 20 seconds after a failure. After it fails three times in a row, it will register a test failure. The regular interval will then resume and retry will be suspended until there is a successful test. This setting can be useful when your system has momentary hiccup as you can be assured the recording of failures will only happen when something is truly wrong.
6. The main point of performing Availability test is to be able to simulate calls into your application from around the world. Clicking the Test Locations button will cause Test Location blade to appear. The location of San Antonio, Texas will be selected by default. There are 16 locations available in total. They can be selected individually (for example, you may wish to create a test for all points within the U.S.) or you can choose them all using the Select/Deselect All check box at the top. Microsoft advises clicking more than one location so you distinguish between website and network issues. Once you have made your selection, click OK to close the blade.
Level 3
Asia Pacific University of Technology & Innovation Page 16 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Level 3
Asia Pacific University of Technology & Innovation Page 17 of 23
7.
Clicking the Success Criteria button will cause Success criteria blade to appear. This section sets the conditions on whether the test is successful or not. The blade will be prepopulated with the HTTP status code check box selected and with a status code of 200 entered. A 200 status code represents a successful HTTP request.
The setting will verify the URL under test will return a 200 OK value. If this occurs the test is successful. Any other return code will result in a test failure. On the flip side, this setting can also be used to test the security of your site. You could create a test that points to a page that needs authorization to view it. Creating a test verifies a 401 Unauthorized is returned shows that the page is still secure and that a setting has not changed.
8.
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
9. A content match setting also exist on this blade. This can be used in conjunction with the HTTP status code or on its own. Content match can be used to make sure the HTTP request returns a string value that matches the text entered into the Content must contain box.
10. The final setting is in the Alerts section. This Alert blade determines whether alerts will be raised on failures and if emails will be generated when they are. They are enabled by default.
11. Once your test has been configured, click Create button at the bottom of the test. 12. Click on the Details button to see the below screenshots.
Level 3
Asia Pacific University of Technology & Innovation Page 18 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
13. As shown below, you can change the showing time range for the availability using the time range button.
Refresh the screen to see the changes of the availability results.
Click on the dot to the specific details for each area.
Level 3 Asia Pacific University of Technology & Innovation Page 19 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
Level 3 Asia Pacific University of Technology & Innovation Page 20 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
j. Load Test with Azure Portal
Estimation time for this section j: 15 Minutes. Setup and run your load performance test.
1. Go to your web app.
2. In the Development Tools section, choose the Performance Test.
3. Now you’ll link an Azure DevOps subscription to keep your performance test history. Choose Set Account.
Level 3 Asia Pacific University of Technology & Innovation Page 21 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
4. If you have an Azure DevOps subscription to use, select that. If you don’t, create a new one.
5. Set the details and run the test. Your web app’s default URL is added automatically. You can change the URL to test other pages (HTTP GET requests only). To simulate local conditions and reduce latency, select a location closest to your users for generating load.
You simulate load on your app by generating virtual users (customers) who visit your web site at the same time. This will show how many requests are failing or responding slowly.
As an example, suppose you have an app that gave out coupons at last year’s holiday sale. This event lasted 15 minutes with a peak load of 100 concurrent customers. You want to double the number of customers this year. You also want to improve customer satisfaction by reducing the page load time from 5 seconds to 2 seconds. So, you can test your updated app’s performance with 250 users for 15 minutes.
6. Watch the progress in real time while the test runs. During the first minute, the page loads slower than is required.
Level 3
Asia Pacific University of Technology & Innovation Page 22 of 23
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Application Insights for ASP .NET Core
7. After the test is done, view the final results. You can see that the page loads much faster after the first minute. This helps identify where you might start troubleshooting the problem.
——-END——
Summary:
In this tutorial, we learned how to use the application insights to monitor the web applications.
Level 3 Asia Pacific University of Technology & Innovation Page 23 of 23