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.
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.
Select WebAPI and click OK.
Open the Solution Explorer. Right-click on the web project, click Manage NuGet Packages.
Search for Microsoft.ApplicationInsights.AspNetCore and click to install the latest version.
Right-click on the web project and click on Confiure Application Insights.
Click on Start Free to configure on Azure, select appropriate resource group and application.
Click on register and the Application Insights are configured for your application.
After this, you see a configuration added to your appsettings.json file.
- "ApplicationInsights": {
- "InstrumentationKey": "your key"
- }
This instrumentation key is like a key to your application insights. Go to project and open Startup.cs file to insert the below code.
- public class Startup
- {
- public Startup(IHostingEnvironment env)
- {
- var builder = new ConfigurationBuilder()
- .SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
- .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
- .AddEnvironmentVariables();
-
- if (env.IsDevelopment())
- {
- builder.AddApplicationInsightsSettings(developerMode: true);
- }
- Configuration = builder.Build();
- }
-
- public IConfiguration Configuration { get; }
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddApplicationInsightsTelemetry(Configuration);
- services.AddMvc();
- }
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.UseMvc();
- }
- }
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.