07-16-2018, 08:53 PM
Exploring Azure App Service – Azure Diagnostics
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png" width="723" height="418" title="" alt="" /></div><div><p>If you’ve followed our previous posts about using Azure App Service to host web apps in the cloud (<span><a href="https://blogs.msdn.microsoft.com/webdev/2018/05/02/exploring-azure-app-service-introduction/">1. Introduction to App Service</a></span>, <span><a href="https://blogs.msdn.microsoft.com/webdev/2018/05/18/exploring-azure-app-service-web-apps-and-sql-azure/">2. Hosting web apps that use SQL</a></span>) you’re already familiar with how easy it is to get an app running in the cloud; but what if the app doesn’t work correctly (it’s crashing, running too slow, etc.)? In this post we’ll explore how to use Azure’s Application Insights to have always on diagnostics that will help you find and fix these types of issues.</p>
<p>Azure Application Insights provides seamless integration with Azure App Service for monitoring, triaging and diagnosing code-level issues. It can easily be enabled on an existing web app with a single button click. Profiler traces and exception snapshots will be available for identifying the lines of code that caused issues. Let’s take a closer look at this experience.</p>
<h2>Enable Application Insights on an existing web app to investigate issues</h2>
<p>We recommend you follow along using your own application, but if you don’t have one readily available we will be using the <span><a href="https://github.com/CawaMS/contoso-sample">Contoso University</a></span> sample. The instructions on how to deploy it to App Service are in the readme file.</p>
<p>If you are following along with Contoso University, you will notice the Instructors page throws errors. In addition, the Courses tab loads a bit slower than others.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png" alt="" width="723" height="418" class="alignnone size-full wp-image-14465" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-1.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-1.png" alt="" width="592" height="528" class="alignnone size-full wp-image-14475" /></a></p>
<p>Let’s turn on Application Insights to investigate. After the app is deployed to App service, Application Insights can be enabled from the App Service portal by clicking the button on the overview blade:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/34.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-2.png" alt="" width="879" height="472" class="alignnone wp-image-14485 size-large" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-3.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-3.png" alt="" width="595" height="639" class="alignnone size-full wp-image-14495" /></a></p>
<p>Fill out the Application Insights enablement blade the following way:</p>
<ul>
<li>Create a new resource, with an appropriate name.</li>
<li>Leave all default options for Code-level diagnostics on. The added section <strong>Code level diagnostics</strong> is on by default to enable Profiler and Snapshot Debugger for diagnosing slow app performance and runtime exceptions.</li>
<li>Click the ‘OK’ button. Once prompted if to restart the web app, click ‘Continue’ to proceed.</li>
</ul>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-4.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-4.png" alt="" width="450" height="81" class="alignnone size-full wp-image-14505" /></a></p>
<p>Once Application Insights is enabled, you will be able to see its connection status<em>. </em>You can now navigate to the Application Insights overview blade by clicking on the link next to the green check mark:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/61.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-5.png" alt="" width="500" height="299" class="alignnone wp-image-14515 size-mediumlarge" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png" alt="" width="676" height="611" class="alignnone size-full wp-image-14305" /></a></p>
<h2>Generate traffic for your web app in order to reproduce issues</h2>
<p>Let’s generate some traffic to reproduce our two issues: a) Courses page loading slowly, b) Instructors page throwing errors. You are going to use <a href="https://docs.microsoft.com/vsts/test/load-test/app-service-web-app-performance-test?view=vsts">performance test with Azure portal</a> to make requests to the following URLs:</p>
<ul>
<li><span>http://<your_webapp_name>.azurewebsites.net/Courses</span></li>
<li><span>http://<your_webapp_name>.azurewebsites.net/Instructors</span></li>
</ul>
<p>You are going to be creating two different performance tests, once for each URL. For the Courses page you are going to simulate 250 users over a duration of 5 minutes, since you are experiencing a performance issue. For the Instructors page you can simulate 10 users over 1 minutes since we’d generally expect less instructors than users on the site at any given time.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-7.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-7.png" alt="" width="273" height="422" class="alignnone size-full wp-image-14535" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-8.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-8.png" alt="" width="630" height="433" class="alignnone size-full wp-image-14555" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-9.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-9.png" alt="" width="556" height="285" class="alignnone size-full wp-image-14565" /></a></p>
<h2>Profile your app during performance tests to identify performance issues</h2>
<p>A performance test will generate traffic, but if you want to understand how you app performs you have to analyze its performance while the test is running using a profiler. Let’s do exactly that, using the Performance | Configure Profiler blade while one of our performance tests is running:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-10.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-10.png" alt="" width="768" height="249" class="alignnone size-full wp-image-14575" /></a></p>
<p>Later on we will walkthrough how to use the profiler to identify the exact lines of code responsible for the slow-down. For now, let’s focus on simply profiling . After clicking on “Profile now”, wait until the performance test finishes and the profiler has completed its analysis. After a new entry in the section “Recent profiling sessions” appears, navigate back to the Overview blade.</p>
<h2>Diagnose runtime exceptions</h2>
<p>If your web app has any failed requests, you need to root cause and solve the issues to ensure your app works reliably. After running your performance test on the Instructors page you have some example failed requests to take a look at:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png" alt="" width="676" height="611" class="alignnone size-full wp-image-14305" /></a></p>
<p>Let’s go to Failures blade to investigate what failed.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-11.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-11.png" alt="" width="756" height="608" class="alignnone size-full wp-image-14315" /></a></p>
<p>On the Failures blade, the top failed operation is ‘GET Instructors/Index’ and the top exception is ‘InvalidOperationException’. Click on the ‘COUNT’ column of the exception count to open list of sample exceptions. Click on the suggested exception to open the End-to-End Transaction blade.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-12.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-12.png" alt="" width="780" height="461" class="alignnone size-full wp-image-14325" /></a></p>
<p>In the End-to-End Transaction blade, you can see ‘System.InvalidOperationException’ exceptions thrown from GET Instructors/Index operation. Select the exception row and click on ‘Open debug snapshot’.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-13.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-13.png" alt="" width="1053" height="462" class="alignnone size-full wp-image-14335" /></a></p>
<p>If it’s the first time you use Snapshot Debugger, you will prompt to request RBAC access to snapshots to safeguard the potentially sensitive data shown in local variables:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/17.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-14.png" alt="" width="500" height="319" class="alignnone wp-image-14345 size-mediumlarge" /></a></p>
<p>Once you get access, you will see a snapshot view like the following:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png" alt="" width="646" height="362" class="alignnone size-full wp-image-14365" /></a></p>
<p>From the local variables and call stack, you can see the exception is thrown at InstructorsController.cs line 56 with message ‘Nullable object must have a value’. This provides sufficient information to proceed with debugging the app’s source code.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png" alt="" width="646" height="362" class="alignnone size-full wp-image-14365" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-16.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-16.png" alt="" width="742" height="638" class="alignnone size-full wp-image-14375" /></a></p>
<p>To get a better experience, download the snapshot and debug it using <span><a href="https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=15">Visual Studio Enterprise</a></span>. You will be able to interactively see which line of code caused the exception:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/211.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-17.png" alt="" width="429" height="350" class="alignnone wp-image-14385 size-mediumlarge" /></a></p>
<p>Looking at the code, the exception was thrown because ‘id’ is null and you are missing the appropriate check. When your app is running in the cloud, resources are dynamically provisioned and destroyed and so may not always get the chance to access the state of a failed component before it is reset. The Snapshot Debugger is a powerful tool that can maintain the failed state of the component even after the component’s state has been reset, by capturing local variables and call stack at the time the exception is thrown.</p>
<h2>Diagnose bad performance from your web app</h2>
<p>The server response time chart on the overview blade provides quick information on how performant your web app is over time. In the Contoso example, we see some spikes on the chart indicating there is a short period of time where users experienced slow responses for the requests they made.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-18.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-18.png" alt="" width="402" height="320" class="alignnone size-full wp-image-14395" /></a></p>
<p>To investigate further, navigate to the Performance blade in App Insights portal:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/231.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-19.png" alt="" width="879" height="568" class="alignnone wp-image-14405 size-large" /></a></p>
<p>Zoom into the time range 6pm-10pm in the ‘Operation times: zoom into range’ chart to see spikes on the Request count.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-20.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-20.png" alt="" width="790" height="351" class="alignnone size-full wp-image-14415" /></a></p>
<p>Sort the Operations table by duration and request count to figure out where your web app spent the most time. In our example GET Courses/Index operation has the longest average duration. Let’s click on it to investigate why this operation takes so long.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-21.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-21.png" alt="" width="358" height="200" class="alignnone size-full wp-image-14425" /></a></p>
<p>The right side of the blade provides insights based on the Course/Index operation.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-22.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-22.png" alt="" width="784" height="665" class="alignnone size-full wp-image-14435" /></a></p>
<p>From the chart you can see although most operations only took around 400ms, the worst ones took up to 35 seconds. This means some users are getting really bad experience when hitting this URL and you should address it. Let’s look at Profiler traces to troubleshoot why GET Courses/Index was being so slow.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-23.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-23.png" alt="" width="364" height="101" class="alignnone size-full wp-image-14445" /></a></p>
<p>You should see profiler traces similar to the following:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/28.png"><img src="https://msdnshared.blob.core.windows.net/media/2018/07/28-1024x450.png" alt="" width="879" height="386" class="alignnone wp-image-14455 size-large" /></a></p>
<p>The code path that’s taking the most time is prefixed with the flame icon. In this particular request, most time was spent on reading the list of courses from the database. Browsing to the code <span><a href="https://github.com/CawaMS/contoso-sample/blob/master/cu-final/Controllers/CoursesController.cs">CourseController.cs</a></span> we can see that when loading the list of courses the AsNoTracking() optimization option is not used. By default, Entity Framework will turn on tracking which caches the results to compare with what’s modified. This could add an approx. ~30% overhead to your app’s performance. For simple read operation like this one, we can optimize the query performance by using the AsNoTracking() option.</p>
<h2>Conclusion</h2>
<p>We hope that you find it easy to use Application Insights to diagnose performance and errors in your web apps. We believe Azure App Service is a great place to get started hosting and maintaining your web apps. You don’t have to enable App Insights upfront; the option is always there to be turned on when and as needed without re-deployment.</p>
<p>If you have any questions or issues, let us know by leaving comments below.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/cath_profile.png"><img src="https://msdnshared.blob.core.windows.net/media/2018/07/cath_profile.png" alt="" width="200" height="200" class="size-full wp-image-14585 alignleft" /></a><strong /></p>
<p><strong>Catherine Wang</strong> Program Manager, VS and .NET</p>
<p>Catherine is on Azure developer experience team and is responsible for Azure diagnostics, security and storage tools.</p>
<p> </p>
</div>
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png" width="723" height="418" title="" alt="" /></div><div><p>If you’ve followed our previous posts about using Azure App Service to host web apps in the cloud (<span><a href="https://blogs.msdn.microsoft.com/webdev/2018/05/02/exploring-azure-app-service-introduction/">1. Introduction to App Service</a></span>, <span><a href="https://blogs.msdn.microsoft.com/webdev/2018/05/18/exploring-azure-app-service-web-apps-and-sql-azure/">2. Hosting web apps that use SQL</a></span>) you’re already familiar with how easy it is to get an app running in the cloud; but what if the app doesn’t work correctly (it’s crashing, running too slow, etc.)? In this post we’ll explore how to use Azure’s Application Insights to have always on diagnostics that will help you find and fix these types of issues.</p>
<p>Azure Application Insights provides seamless integration with Azure App Service for monitoring, triaging and diagnosing code-level issues. It can easily be enabled on an existing web app with a single button click. Profiler traces and exception snapshots will be available for identifying the lines of code that caused issues. Let’s take a closer look at this experience.</p>
<h2>Enable Application Insights on an existing web app to investigate issues</h2>
<p>We recommend you follow along using your own application, but if you don’t have one readily available we will be using the <span><a href="https://github.com/CawaMS/contoso-sample">Contoso University</a></span> sample. The instructions on how to deploy it to App Service are in the readme file.</p>
<p>If you are following along with Contoso University, you will notice the Instructors page throws errors. In addition, the Courses tab loads a bit slower than others.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics.png" alt="" width="723" height="418" class="alignnone size-full wp-image-14465" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-1.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-1.png" alt="" width="592" height="528" class="alignnone size-full wp-image-14475" /></a></p>
<p>Let’s turn on Application Insights to investigate. After the app is deployed to App service, Application Insights can be enabled from the App Service portal by clicking the button on the overview blade:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/34.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-2.png" alt="" width="879" height="472" class="alignnone wp-image-14485 size-large" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-3.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-3.png" alt="" width="595" height="639" class="alignnone size-full wp-image-14495" /></a></p>
<p>Fill out the Application Insights enablement blade the following way:</p>
<ul>
<li>Create a new resource, with an appropriate name.</li>
<li>Leave all default options for Code-level diagnostics on. The added section <strong>Code level diagnostics</strong> is on by default to enable Profiler and Snapshot Debugger for diagnosing slow app performance and runtime exceptions.</li>
<li>Click the ‘OK’ button. Once prompted if to restart the web app, click ‘Continue’ to proceed.</li>
</ul>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-4.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-4.png" alt="" width="450" height="81" class="alignnone size-full wp-image-14505" /></a></p>
<p>Once Application Insights is enabled, you will be able to see its connection status<em>. </em>You can now navigate to the Application Insights overview blade by clicking on the link next to the green check mark:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/61.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-5.png" alt="" width="500" height="299" class="alignnone wp-image-14515 size-mediumlarge" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png" alt="" width="676" height="611" class="alignnone size-full wp-image-14305" /></a></p>
<h2>Generate traffic for your web app in order to reproduce issues</h2>
<p>Let’s generate some traffic to reproduce our two issues: a) Courses page loading slowly, b) Instructors page throwing errors. You are going to use <a href="https://docs.microsoft.com/vsts/test/load-test/app-service-web-app-performance-test?view=vsts">performance test with Azure portal</a> to make requests to the following URLs:</p>
<ul>
<li><span>http://<your_webapp_name>.azurewebsites.net/Courses</span></li>
<li><span>http://<your_webapp_name>.azurewebsites.net/Instructors</span></li>
</ul>
<p>You are going to be creating two different performance tests, once for each URL. For the Courses page you are going to simulate 250 users over a duration of 5 minutes, since you are experiencing a performance issue. For the Instructors page you can simulate 10 users over 1 minutes since we’d generally expect less instructors than users on the site at any given time.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-7.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-7.png" alt="" width="273" height="422" class="alignnone size-full wp-image-14535" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-8.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-8.png" alt="" width="630" height="433" class="alignnone size-full wp-image-14555" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-9.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-9.png" alt="" width="556" height="285" class="alignnone size-full wp-image-14565" /></a></p>
<h2>Profile your app during performance tests to identify performance issues</h2>
<p>A performance test will generate traffic, but if you want to understand how you app performs you have to analyze its performance while the test is running using a profiler. Let’s do exactly that, using the Performance | Configure Profiler blade while one of our performance tests is running:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-10.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-10.png" alt="" width="768" height="249" class="alignnone size-full wp-image-14575" /></a></p>
<p>Later on we will walkthrough how to use the profiler to identify the exact lines of code responsible for the slow-down. For now, let’s focus on simply profiling . After clicking on “Profile now”, wait until the performance test finishes and the profiler has completed its analysis. After a new entry in the section “Recent profiling sessions” appears, navigate back to the Overview blade.</p>
<h2>Diagnose runtime exceptions</h2>
<p>If your web app has any failed requests, you need to root cause and solve the issues to ensure your app works reliably. After running your performance test on the Instructors page you have some example failed requests to take a look at:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-6.png" alt="" width="676" height="611" class="alignnone size-full wp-image-14305" /></a></p>
<p>Let’s go to Failures blade to investigate what failed.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-11.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-11.png" alt="" width="756" height="608" class="alignnone size-full wp-image-14315" /></a></p>
<p>On the Failures blade, the top failed operation is ‘GET Instructors/Index’ and the top exception is ‘InvalidOperationException’. Click on the ‘COUNT’ column of the exception count to open list of sample exceptions. Click on the suggested exception to open the End-to-End Transaction blade.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-12.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-12.png" alt="" width="780" height="461" class="alignnone size-full wp-image-14325" /></a></p>
<p>In the End-to-End Transaction blade, you can see ‘System.InvalidOperationException’ exceptions thrown from GET Instructors/Index operation. Select the exception row and click on ‘Open debug snapshot’.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-13.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-13.png" alt="" width="1053" height="462" class="alignnone size-full wp-image-14335" /></a></p>
<p>If it’s the first time you use Snapshot Debugger, you will prompt to request RBAC access to snapshots to safeguard the potentially sensitive data shown in local variables:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/17.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-14.png" alt="" width="500" height="319" class="alignnone wp-image-14345 size-mediumlarge" /></a></p>
<p>Once you get access, you will see a snapshot view like the following:</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png" alt="" width="646" height="362" class="alignnone size-full wp-image-14365" /></a></p>
<p>From the local variables and call stack, you can see the exception is thrown at InstructorsController.cs line 56 with message ‘Nullable object must have a value’. This provides sufficient information to proceed with debugging the app’s source code.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-15.png" alt="" width="646" height="362" class="alignnone size-full wp-image-14365" /></a></p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-16.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-16.png" alt="" width="742" height="638" class="alignnone size-full wp-image-14375" /></a></p>
<p>To get a better experience, download the snapshot and debug it using <span><a href="https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Enterprise&rel=15">Visual Studio Enterprise</a></span>. You will be able to interactively see which line of code caused the exception:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/211.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-17.png" alt="" width="429" height="350" class="alignnone wp-image-14385 size-mediumlarge" /></a></p>
<p>Looking at the code, the exception was thrown because ‘id’ is null and you are missing the appropriate check. When your app is running in the cloud, resources are dynamically provisioned and destroyed and so may not always get the chance to access the state of a failed component before it is reset. The Snapshot Debugger is a powerful tool that can maintain the failed state of the component even after the component’s state has been reset, by capturing local variables and call stack at the time the exception is thrown.</p>
<h2>Diagnose bad performance from your web app</h2>
<p>The server response time chart on the overview blade provides quick information on how performant your web app is over time. In the Contoso example, we see some spikes on the chart indicating there is a short period of time where users experienced slow responses for the requests they made.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-18.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-18.png" alt="" width="402" height="320" class="alignnone size-full wp-image-14395" /></a></p>
<p>To investigate further, navigate to the Performance blade in App Insights portal:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/231.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-19.png" alt="" width="879" height="568" class="alignnone wp-image-14405 size-large" /></a></p>
<p>Zoom into the time range 6pm-10pm in the ‘Operation times: zoom into range’ chart to see spikes on the Request count.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-20.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-20.png" alt="" width="790" height="351" class="alignnone size-full wp-image-14415" /></a></p>
<p>Sort the Operations table by duration and request count to figure out where your web app spent the most time. In our example GET Courses/Index operation has the longest average duration. Let’s click on it to investigate why this operation takes so long.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-21.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-21.png" alt="" width="358" height="200" class="alignnone size-full wp-image-14425" /></a></p>
<p>The right side of the blade provides insights based on the Course/Index operation.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-22.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-22.png" alt="" width="784" height="665" class="alignnone size-full wp-image-14435" /></a></p>
<p>From the chart you can see although most operations only took around 400ms, the worst ones took up to 35 seconds. This means some users are getting really bad experience when hitting this URL and you should address it. Let’s look at Profiler traces to troubleshoot why GET Courses/Index was being so slow.</p>
<p><a href="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-23.png"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/exploring-azure-app-service-azure-diagnostics-23.png" alt="" width="364" height="101" class="alignnone size-full wp-image-14445" /></a></p>
<p>You should see profiler traces similar to the following:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/28.png"><img src="https://msdnshared.blob.core.windows.net/media/2018/07/28-1024x450.png" alt="" width="879" height="386" class="alignnone wp-image-14455 size-large" /></a></p>
<p>The code path that’s taking the most time is prefixed with the flame icon. In this particular request, most time was spent on reading the list of courses from the database. Browsing to the code <span><a href="https://github.com/CawaMS/contoso-sample/blob/master/cu-final/Controllers/CoursesController.cs">CourseController.cs</a></span> we can see that when loading the list of courses the AsNoTracking() optimization option is not used. By default, Entity Framework will turn on tracking which caches the results to compare with what’s modified. This could add an approx. ~30% overhead to your app’s performance. For simple read operation like this one, we can optimize the query performance by using the AsNoTracking() option.</p>
<h2>Conclusion</h2>
<p>We hope that you find it easy to use Application Insights to diagnose performance and errors in your web apps. We believe Azure App Service is a great place to get started hosting and maintaining your web apps. You don’t have to enable App Insights upfront; the option is always there to be turned on when and as needed without re-deployment.</p>
<p>If you have any questions or issues, let us know by leaving comments below.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/2018/07/cath_profile.png"><img src="https://msdnshared.blob.core.windows.net/media/2018/07/cath_profile.png" alt="" width="200" height="200" class="size-full wp-image-14585 alignleft" /></a><strong /></p>
<p><strong>Catherine Wang</strong> Program Manager, VS and .NET</p>
<p>Catherine is on Azure developer experience team and is responsible for Azure diagnostics, security and storage tools.</p>
<p> </p>
</div>