Configure Application Insight For .NET Core 2.0

Why?

We developers work hard to write code but these QA guys find bugs in our code. We don’t have any choice but to find and fix the bug ASAP. After all this hard work, we push everything to production, then new questions arise from people - why is this page slow? why is this operation taking time? why this and that? To answer these questions, we need a proper logging mechanism to identify and it takes hours of coding and analysis. What if we had a mechanism to track this with fewer hours of code to analyze and more time to fix it? Application insights come into the picture to solve our logging work with best analysis tools.

What is required to proceed?

We need an Azure subscription to start with Application Insights to our applications. All the logs are recorded here and we can see our data.

How costly is this?

For the first 1GB, it is free and after that, it costs INR. 152 per GB. 

.NET Core

To use AppInsigts, should the application be in Azure?

Not at all. Your application could be running in your local development environment or hosted in some third party hosting environments or Azure or AWS or Google Cloud, no matter where it is running, it will log the data. But we require the internet to log into the server. To use this feature, we need to configure the Application Insigts in our application and put some code depending on our language.
 
Does it only support .NET?

No, it supports most commonly used languages. Please refer to App Insights support for more details.
 
Configuring Aplication Insights for AP.NET Core 2.0 

Create a project.

.NET Core

Select WebAPI and click OK.

.NET Core

Open the Solution Explorer. Right-click on the web project, click Manage NuGet Packages.
 
.NET Core
 
Search for Microsoft.ApplicationInsights.AspNetCore and click to install the latest version.

.NET Core

Right-click on the web project and click on Confiure Application Insights.
 
.NET Core
 
Click on Start Free to configure on Azure, select appropriate resource group and application.
 
.NET Core
 
Click on register and the Application Insights are configured for your application.
 
After this, you see a configuration added to your appsettings.json file.
  1. "ApplicationInsights": {  
  2.     "InstrumentationKey": "your key"  
  3.   }  
This instrumentation key is like a key to your application insights. Go to project and open Startup.cs file to insert the below code.
  1. public class Startup  
  2.     {  
  3.         public Startup(IHostingEnvironment env)  
  4.         {  
  5.             var builder = new ConfigurationBuilder()  
  6.                 .SetBasePath(env.ContentRootPath)  
  7.                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)  
  8.                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)  
  9.                 .AddEnvironmentVariables();  
  10.   
  11.             if (env.IsDevelopment())  
  12.             {  
  13.                 builder.AddApplicationInsightsSettings(developerMode: true);  
  14.             }  
  15.             Configuration = builder.Build();  
  16.         }  
  17.   
  18.         public IConfiguration Configuration { get; }  
  19.   
  20.         // This method gets called by the runtime. Use this method to add services to the container.  
  21.         public void ConfigureServices(IServiceCollection services)  
  22.         {  
  23.             services.AddApplicationInsightsTelemetry(Configuration);
  24.             services.AddMvc();  
  25.         }  
  26.   
  27.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  28.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  29.         {  
  30.             if (env.IsDevelopment())  
  31.             {  
  32.                 app.UseDeveloperExceptionPage();  
  33.             }  
  34.   
  35.             app.UseMvc();  
  36.         }  
  37.     }  
Note

If you're using version 2.0, you must not include the following.
  • app.UseApplicationInsightsRequestTelemetry();
  • and app.UseApplicationInsightsExceptionTelemetry(); since they are managed internally.
That's it?

A big yes, but we didn't write any code to catch the exceptions and request logs. How will it work? 
 
That is the advantage with .NET core. Everything is indepedent from each other, we are adding ApplicationInsigts to the middleware(In start up file) application pipeline. Hence, this will read the incoming requests and exceptions automatically with no code in our Controllers. If you want to write some extra custom events and logs you can refer to this link.
 
Where can I check these logs?
 
Go to Azure portal and More services, search for Application Insigts and select the name you have given at the time of your Application Insigts configuration. You can check all the information here.
 
.NET Core