Configure and analyze Application Insight logs of an ASP.NET Core application

Application insight is an Azure monitoring tool that we can use to inspect the behaviours of .NET application. Based on the logs and the exceptions, it’s collect and categorize the datas, that are available for analysis by the developers who want to understand what is the cause of an anomaly or simply know the healty of the application.

We can view the collected data locally when we debug the application or on the Azure portal if we have choosen this option. We can configure Application Insight on classic ASP.NET applications or on ASP.NET Core applications as well. In this post we’ll see how we can configure it on an ASP.NET Core application for local debugging or for the production environment. We’ll also see how we can analyze the collected data, by simply using the default views or with the Log Analytics query language.

Debugging configuration

The first window that appear when we try to add Application Insight is the registration window:

If we want to simply configure AI for debugging purphose, we have not to configure an Azure account but we can simply add the SDK, by clicking the link at the bottom of the window.

The result is that a new package will be added to the csproj file:

Now we are ready to monitor the application. The first step is a start the application in debug mode and then click in Visual Studio the Application Insights Search Telemetry button (in the Application Insights submenu), we’ll see the telemetry informations starting from the application startup:

As you can see, we could filter by the telemetry tipology (Exceptions, Custom events, Requests, Traces), time range and so on. We have a large set of informations available, that we can use, for example, to determine the efficiency of our application (we have the duration of all the tracked events). On the left side we have other filters to restrict the result subset.

All of this without configure an Azure account, so without additional expenses of the Azure portal. As we’ll see in the local version some features are missing and the configuration is available only for debug purphose.

Azure Portal configuration

So if we want to configure Application Insights on the Azure portal, we have to go back on the configuration window and specify the additional datas about Account, Subscription and Resource:

After that we’ll find the new resource in the list of the resources on the Azure portal:

As said we have more basic statistics available, about failed requests, exceptions, performance metrics, availability.

In many cases the visualization offered by the portal is very exaustive and sufficient to analyze performances and problems. We have to know the organization of the informations and where we can find what we need. Anyway we have another method to retrieves metrics informations, that programmers could like, that is the Logs Analytics query language.

Query language

In the main reports pages there is an Analytics button that we can use to access query language window:

 

It’s an high level language that we can use to access all the metrics informations available and to filter a subset of them:

All the informations are organized in subsets, based on their tipology. We can have Requests, PageViews, Exception and we can find the tipologies on the left side of the analytics window.

For example, if we select page views, the first part of the query will be populated with the tipology selected:

We can refine the query by adding additional conditions with the | character. The intellisense is awesome and it’s very intuitive to write query and filter data. We have a lot of informations available and we can see part of them in the result view. We have informations about the date/time of the event, the name of the project, the page url, time to complete the operation and a useful performance categorization. We can customize this view as well with the informations that we prefer.

This is an example of a complex query:

// exception count by problem ID
let start=datetime("2018-09-18T06:31:00.000Z");
let end=datetime("2018-09-19T06:31:00.000Z");
let timeGrain=5m;
let dataset=exceptions
// additional filters can be applied here
| where timestamp > start and timestamp < end
| where client_Type != "Browser" ;
dataset
// change 'problemId' on the below line to segment by a different property
| summarize count_=sum(itemCount), impactedUsers=dcount(user_Id) by problemId
// calculate exception count for all exceptions
| union(dataset
| summarize count_=sum(itemCount), impactedUsers=dcount(user_Id)
| extend problemId="Overall")
| where count_ > 0
| order by count_ desc

As you can see, the syntax is readable and easily to understand.

Summary

We’ve seen how we can configure Application insights SDK for debugging and how we can see the information metrics locally with Visual Studio. Once we need a production configuration or simply we want to switch to the Azure Portal, we can add the additional configuration about the Azure Portal subscription. The portal offer more advanced statistics and graphs, we can find informations about requests, performances, errors.

If we prefer custom filters, we can use the Log Analytics query language, an high level language that we can use to do queries and filters. A awesome intellisense help us to write these queries very quickly.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s