Tuesday, 20 January 2026

How to use Application Insights in ASP.Net Core

In this tutorial we are going to see how to use the Application Insights in asp.net core. and also explain about how to use some features of application insights. First we see few steps for basic level of logging, then we will see other features present in application insights.

Additional Features:

  1. Use Custom Properties
  2. Log Different messages
  3. Controller Level Configuration
  4. Remove or Filter Modules

Steps for Basic Level of Logging:
First we have to install Microsoft.ApplicationInsights.AspNetCore in Asp.Net Core Project. Configure program.cs like below to use application insights.

           builder.Services.AddApplicationInsightsTelemetry();

The Default LogLevel for ApplicationInsightsLoggerProvider is Warning.  if you try to Log Debug or Trace it won't Log, because default level is Warning.
 
Setting Log Level of Default Provider (ApplicationInsightsLoggerProvider) to Debug
will get change based on the Environment you deploy your application, so load the
LogLevel from configuration and set to Default Provider (ApplicationInsightsLoggerProvider).


Before that we have know that LogLevel will work in Bottom to Top order. If you give value of enum Critical as LogLevel, then only Critical will log. if you give LogLevel value as Warning, then Logs from "Critical", "Error" and "Warning" will Log. it will log from Bottom to Top approach until which LogLevel will you give. So the Default Value now is Warning, to log Debug, we have to remove the default Rule and create one. When you give Empty value in AddFilter for first param then it will take default provider with log level what we mention in second parameter.




Above code will make default log level to Debug, so now up to Log Debug will work. Now you can log the messages in your code, Make sure you are getting the log level from configuration File. Now we see different features present in Application Insights, above 2 steps is enough to log messages up to Debug Level, if you don't want Log Debug, then change that to higher level like LogLevel.Error for AddFilter of ApplicationInsightsLoggerProvider.


1. Use Custom Properties
we will take a example of custom TelemetryProcesssor where custom properties for each HttpRequest will be logged.


2. Log Different Messages
We have to inject ILogger<ControllerName> then use the object and call the methods inside that. Here we are using two methods LogDebug and LogError.



3. Controller Level Configuration
How to set the LogLevel configuration at controller level, we have to do the below config in program.cs. You can see that AuthorController logs only Error, even though if you have LogDebug method  in code, but in insights it wont get logged.


4. Remove or Filter Modules
In Module level we can filter the messages like in Dependencies are logged in application insights, if we don't want SQL Text from EF core wont need to log then configure EnableSqlCommandTextInstrumentation item as "false". 



if you don't want the Dependencies are not allow to log then we have to remove the module like below in Program.cs.



Before Remove Module: you can see Dependency, After Remove Module: you can see Dependency(0). From this tutorial you can learn how to use the Application Insights in Asp.Net Core.