App insights is very powerful logging and monitoring azure service which help us keep track of traces, logging and exceptions of our web app.
App insights have many more powerful feature, we’ll focus on understanding how we can enable application insights in our webapp and check our traces/logs/exceptions :-
Step 1: Create a App service in Azure:
1.1 Navigate to your resource group and create a new web app.
1.2 Select a unique Name, runtime stack and region close to you as shown in below image.
1.3 Click on Next: Monitoring and select a name for your new application insights instance. You can select your existing application insights instance if you want to use that.
1.4 Click on Review + Create and wait till your app service is deployed.
Step 2: Check if your app service/web app is connected to app insights
2.1 Go to your web app and click on configuration on left blade. You should see below 3 application settings automatically populated:

2.2 Click on application insights and you should be able to see it enabled and name of your application insights instance would be visible as shown below:

Step 3: Create an ASP.Net Web Application in Visual studio:
3.1 Create an ASP.Net Web Application Project in Visual studio
3.2 Add below Nuge packages from package manager:
a) Microsoft.ApplicationInsights (Needed to log app insight telemetry data)

b) Microsoft.ApplicationInsights.TraceListener (Used for logging System.Diagnostics.Trace in app insights)

c)Microsoft.ApplicationInsights.PerfCounterCollector ( Used for checking live metrices in app insights)

Alternatively you can open NuGet Package Manager console from Tools>NuGet Package Manager> Package Manager Console and run below 3 commands:
Install-Package Microsoft.ApplicationInsights -Version 2.15.0
Install-Package Microsoft.ApplicationInsights.TraceListener -Version 2.16.0-beta1
Install-Package Microsoft.ApplicationInsights.PerfCounterCollector -Version 2.16.0-beta1
3.3: Update the code of Get method in valuecontroller with below code.
It’s a simple code which divide 100 by value passed to the controller.
public int Get(int id)
{
int result = 0;
try
{
System.Diagnostics.Trace.TraceInformation("Starting calculation, Value supplied "+ id);
if (id > 100)
{
System.Diagnostics.Trace.TraceWarning("Warning: Supplied Value "+ id + " is greater than allowed value 100");
}
result = 100 / id;
}
catch (Exception ex)
{
System.Diagnostics.Trace.TraceError("An error eoccured.");
telemetryClient.TrackException(ex);
}
return result;
}
Also place a private variable and constructor in your code as below:
private static readonly TelemetryClient telemetryClient;
static ValuesController()
{
telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
}
You ValueController class should look like :

Step 4: Publish your applciation
4.1 Navigate to your app service in azure and click on Get Publish profile in overview blade:

4.2 Go to visual studio project and right click on project and click on publish:

4.3: Click on import profile and select the file downloaded in step 4.1

4.4: Click on publish and wait for application to publish to azure:

Step 5: Test the tracing
5.1: For checking traces, go to azure and paste below url in new tab
https://demo365.azurewebsites.net/api/values/5 (Don’t forget to change demo365 in url with your app service name..!)

Navigate to your app insights instance and click on live metrices:

5.2 For testing warning supply value over 100 to value controller


In live metrices, you can see warning trace
Click on search to see all logs in a timelines, you would be able to see normal information log and Warning trace as shown below:

5.3 For testing exception supply value 0 to value controller


Navigate to Search and you should be able to see the exception and well as trace which got logged from catch block of our code.

So this was a glimpse of how powerful app insight is for logging and monitoring purpose.
For understanding more feature of app insights What is Azure Application Insights? – Azure Monitor | Microsoft Docs
Happy learning..!
Leave a Reply