Application Insights In MVC Core And Web API Core

Introduction 

 
In this article, we learn about how to use application insights in local environments for ASP Core applications, configuration, and setup from end to end.
 
Prerequisites
  1. MVC Core
  2. Web API Core
  3. Console Application 
Follow the below steps.
 
Step 1
  1. Open Visual Studio 2019 -> Goto File --> Project ,
  2. Search 'Blank Solution' in the search bar to further filter the templates,
  3. Select 'Blank Solution' and Click on the 'Next' option, as shown below.
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 2
 
Provide a valid solution name your blank solution, choose a disk location for the solution code path, and click on the 'Create' button.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 3
 
Now an Empty Solution was created. Next, create a new MVC core web application by right-clicking on the solution, which is shown below. 
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 4
 
As shown in the below image, we have the option to create a new project. So, name your project and click on the 'Create' button. 
 
If you want to add an additional project to a solution, right-click the solution node in Solution Explorer and choose Add > New Project .
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 5
 
Select .NET Core and ASP.NET Core 3.1 as shown in below filters and Select MVC Web application and click on the 'Create' button as shown in the below screengrab. 
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 6
 
If you want to add an additional project to a solution, right-click the solution node in Solution Explorer and choose Add > New Project .
 
Now we are required to have one more application for API. Select valid filters and Web API project template, then click on the 'Create' button.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 7
 
Now go to your VS 2019, right-click on my solution and select ‘Add Solution To Source Control’ for a solution path to push my code in GITHUB.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 8
 
Go to Team explorer -- > connect symbol --> select valid email id's to create a new repository and click on publish.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 9
 
Now we successfully pushed code to the GITHUB repository, 
 
Note: If you've any issues with code pushing, the error will be logged in the output panel in Visual Studio 2019
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 10
 
Add a model class in the Model folder in Web API project for Department entities.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 11
 
Add a model class in the Model folder in Web API project for Employee entities.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 12
 
Add Repository Folder for DBContext class, Add Employees and Departments references as shown in the below screenshot.
 
Note: Use migration commands by configuring valid DB connection string. I'm using free SQL server services from here.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 13
 
Add API Controller for Employee and Department using EF, as shown in the below screenshots. 
  • Right-Click on your Controllers Folder in the project node. Click on Add New Item, and select the API controller with actions using the entity framework. Then click on 'Add' button
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Department Controller
 
Select the Department Model and Data context class i.e., 'DBContext'. The controller name is Departments Controller.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Department Controller
  • Use CRUD operations which are created by Department Controller Creation Process. 
Application Insights in MVC Core + Web API Core + Entity Framework Core
Step 14 - Employee Controller 
  • Follow Step 13 process for the Employee Controller creation process. 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 15
 
Now install application insights references through the NuGet package manager, as shown in the below screenshot. 
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 16
 
Click ‘OK’ to confirm dependencies for AI. ASP.NET Core dependencies is prompted, as shown below. 
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 17
 
Click on the ‘I Accept’ button for license acceptance.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 18
 
Now configure the application instrumentation key in your appsettings.json file. (If you’re using .net framework, you need to configure in web.config file under the app settings section).
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Step 19
 
Add application insights telemetry in Startup.cs, as shown in the below screenshot.
 
Application Insights in MVC Core + Web API Core + Entity Framework Core
 
Now the Local Environment setup has been completed for Application Insights learning. In the next article, we'll look into more about how Application Insights performance and help the developer fix bugs by using Visual Studio 2019. 
 


Similar Articles