Application Insight In Microsoft Azure

One of the toughest tasks in the development life cycle begins after deployment to monitor the performance of the application in order to know how it’s behaving on the server: whether it's  working properly or if some services have stopped and many more scaling problems. Microsoft Azure has given us a very very useful service to handle all such problems – Application Insight.

Application Insight is an analytics service that continuously monitors your live Web Application. It monitors clients, servers and any third party services you are using. It also detects triage, diagnoses problems, system failures, detects performance issues, and has proactive detection in case of any failure. This also provides the capabilities to write your own events, metrics, and traces. Thus, basically we can say that Application Insight is designed to make the developers' lives easy and helps continuously to improve the availability, performance and usability of our app. "App Insights" is not only a code based plugin, it's a lot more. It provides you with a very rich user interface, where you can analyze, filter and segment the data. You can also make custom dashboards.

The Prerequisites for  Application Insights are,

  1. Visual Studio 2013 Service pack 3 or later
  2. A subscription to Microsoft Azure
  3. Azure Cloud Services

To add Application Insight in any existing .NET Web Application, right click on the Solution and make use of Add application Insight telemetry, as shown below: 

Add Application

Afterwards, you will see the following login screen: 

login

login

Thus, the command did three things:

  1. Added the Application Insights Web SDK NuGet package to your project. To see it in Visual Studio, right-click your project and choose Manage NuGet Packages.

    Application Insights Web

  2. Create an Application Insights resource in the Azure portal. This is where you'll see your data. It retrieves the instrumentation key, which identifies the resource.

    After this, you will see that the ApplicationInsights.config is added in your solution.

    After this integration is done successfully, it automatically tracks telemetry from your Application and its context.

Telemetry initializers set the context properties that are sent along with the every item of telemetry.

We can write our own Initializers to set the context properties.

Public class AppInsightsContextInitializer

  1. Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer  
  2.     {  
  3.         public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)  
  4.         {  
  5.             telemetry.Context.Component.Version = typeof(MvcApplication).Assembly.GetName().Version.ToString();  
  6.             telemetry.Context.Properties["AppName"] = “abc”  
  7.         }  
  8.     } 
In Global.ascx file, you can write the code to initialize,
  1. protected void Application_Start()  
  2.        {  
  3.   
  4.            TelemetryConfiguration.Active.TelemetryInitializers.Add(new AppInsightsContextInitializer());  
  5.            TelemetryConfiguration.Active.InstrumentationKey = “Your Instrumentation Key”  
  6. }  
  7. JavaScript telemetry initializers - Insert a telemetry initializer for javascripts  
  8. <script type="text/javascript">  
  9.         // ... initialization code  
  10.         ...({  
  11.             instrumentationKey: "your instrumentation key"  
  12.         });  
  13.         window.appInsights = appInsights;  
  14.   
  15.   
  16.         // Adding telemetry initializer.  
  17.         // This is called whenever a new telemetry item  
  18.         // is created.  
  19.   
  20.         appInsights.queue.push(function () {  
  21.             appInsights.context.addTelemetryInitializer(function (envelope) {  
  22.                 var telemetryItem = envelope.data.baseData;  
  23.   
  24.                 // To check the telemetry item’s type - for example PageView:  
  25.                 if (envelope.name == Microsoft.ApplicationInsights.Telemetry.PageView.envelopeType) {  
  26.                     // this statement removes url from all page view documents  
  27.                     telemetryItem.url = "URL CENSORED";  
  28.                 }  
  29.   
  30.                 // To set custom properties:  
  31.                 telemetryItem.properties = telemetryItem.properties || {};  
  32.                 telemetryItem.properties["globalProperty"] = "boo";  
  33.   
  34.                 // To set custom metrics:  
  35.                 telemetryItem.measurements = telemetryItem.measurements || {};  
  36.                 telemetryItem.measurements["globalMetric"] = 100;  
  37.             });  
  38.         });  
  39.   
  40.         // End of inserted code.  
  41.   
  42.         appInsights.trackPageView();  
  43.     </script>  
Once it is installed, you can see the app insight details.

details

This will redirect to the Azure Portal site, where you can login and see the Application performance details.

Thus, the Application Insights do these three things mainly:

 

  1. Detect Server side Performance, issue failure.
  2. Monitor client Side Application (JS).
  3. Provide telemetric data.

Please follow this for more details.


Similar Articles