Setting up Application Insights in ASP.NET Core Application

To set up Application Insights, we first have to log in to the Azure portal and add a new item, Application Insights, as follows:

Select Application Insights and click on create. Once this is done, we have to copy the INSTRUMENTATION KEY, which we can provide in our ASP.NET Core application. You can copy the INSTRUMENTATION KEY by clicking on Essentials in Application Insight on the Azure portal.

Next, we will add the Application Insights instrumentation package in our ASP.NET Core package, which actually monitors our app, and send its report to the Azure portal, where Application Insights is configured.

Add the following package from NuGet package manager:

Microsoft.ApplicationInsights.AspNetCore

Now we have to specify the instrumentation key in our ASP.NET Core appsettings.json file. To do this, create a file, appsettings.json, in the root folder of your web application, and then write the following code snippet, which holds your InstrumentationKey. The configuration snippet of appsettings.json is as follows:

    { 
{
"ApplicationInsights": {
"InstrumentationKey": "a79a8184-a86e-4d8b-b694-8497436a5ebe"
}
}
}

To read appsettings.json, we have to write some code to build the configuration system that will read the settings from different sources (in our case appsettings.json), and give us the Configuration object that can be used to retrieve the settings from key/value pairs. To do this, we will add following two packages from NuGet package manager:

    Microsoft.Extensions.Configuration.Abstractions 
Microsoft.Extensions.Configuration.Json

Next, we will add the following code snippet in our Startup class:

    var builder = new ConfigurationBuilder() 
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true,
reloadOnChange: true)
.AddJsonFile($"appsettings.
{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();

In the preceding code, we read the JSON file and environment variables on the Windows machine, and build a configuration. Configuration is the IConfigurationRoot property defined in our Startup class, as shown next:

    public IConfigurationRoot Configuration { get; }

Now, to use the ApplicationInsights service, we have to add ApplicationInsightsTelemetry in our ConfigureServices method of the Startup class, as follows:

    services.AddApplicationInsightsTelemetry(Configuration);

Finally, we can add this as middleware in our Configure method, as shown next. It's recommended to add the this middleware as the very first item in the pipeline, otherwise proper reporting will not be done:

    app.UseApplicationInsightsRequestTelemetry(); 
app.UseApplicationInsightsExceptionTelemetry();

UseApplicationInsightsRequestTelemetry is used to monitor and get the telemetry information of all the requests being processed, whereas UseApplicationInsightsExceptionTelemetry can be used to monitor exceptions.

Now you can run your application and monitor the performance on Application Insights:

Application Insights provides different options to view telemetry information. There is a search explorer, which can be used to diagnose actual requests, traces, and exceptions. Metrics explorer can be used to diagnose response times, user counts, and page views. Analytics is used to write queries to fetch information related to telemetry, and through set alerts, you can specify alerts that send notifications on special conditions based on your criteria.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.148