Wednesday, September 22, 2021

Integrate App Insights with Linux .NET Web App

Application Insights is a useful tool to monitor service performance whether for data analysis or debugging purpose. While it comes with Windows-based .NET Web Apps by default when creating the app from Azure Portal, it is missing on Linux platform. This article will give instructions on how to integrate App Insights with Linux .NET Web App so that we can enjoy the similar experience on Linux. 

 

Generally speaking, with Linux .NET Web Apps, we need to leverage App Insights SDK to collect telemetries and send them over to App Insights server. After that, App Insights Portal page will be able to retrieve and display metrics and logs.

 

Using .NET 5 MVC app as an example, only 6 steps needed to enable server-side monitoring:

1. Create Application Insights resource via Azure Portal. Normally, make App Insights in the same region with the Web App so that data transmission can be faster.

 

YangYu_0-1632330452770.png

 

2. Jot down the Instrumentation Key on the Overview blade of the App Insights created.

 

YangYu_2-1632330914355.png

 

3. Add app setting “APPINSIGHTS_INSTRUMENTATIONKEY = YourAppInsightsInstrumentationKey” to your web app.

 

YangYu_1-1632330738279.png

 

4. Add below reference in your project's .csproj file.

 

<ItemGroup>

<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />

</ItemGroup>

 

5. Add services.AddApplicationInsightsTelemetry(); to the ConfigureServices() method in your Startup class.

 

public void ConfigureServices(IServiceCollection services)

{

// The following line enables Application Insights telemetry collection.

services.AddApplicationInsightsTelemetry();

 

// This code adds other services for your application.

services.AddMvc();

}

 

6. Rebuild and redeploy your web app.

 

If client-side telemetry is also desired, there are additional 2 steps before recompiling the code:

1. In _ViewImports.cshtml, add injection:

 

@inject MicrosoftApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet

 

2. In _Layout.cshtml, insert HtmlHelper at the end of the <head> section but before any other script.

 

<head>

...

@Html.Raw(JavaScriptSnippet.FullScript)

</head>

 

A common use case is troubleshooting site slowness. Performance bottleneck can be narrowed down with the help of App Insights End-to-end transaction details. With the transaction flow in place, we can easily tell if bottleneck is with the application itself or some external dependencies like database. In other words, we can ensure our next investigation is on the right track.

 

YangYu_3-1632332331560.png

 

 

References:

Azure Application Insights for ASP.NET Core applications - Azure Monitor | Microsoft Docs

 

 

 

Posted at https://sl.advdat.com/3AyN6iC