Azure Application Insights

Introduce and Practice

LAI TOCA
2 min readMay 13, 2020
Ref: https://www.systemcenterautomation.com/2019/02/website-availability-application-insights/

Tracking and monitoring application(s) would be routine works of DevOps daily life. Logging exceptions and interesting events into file system (local drive) was the most common tricky for developers. Main derived problem of storing inside local drive would occurred physical space limitation and hard to do content searching and analyzing.

Azure Application Insights would be the other solution for handling tracing status, user interactivity and performance of application(s). The Application Insight most compatible with Azure Monitor Resource under Azure portal (Right, this cause extra free charged). The benefit of using Application Insight were this framework help us record any un-handle/handle errors and performance anomalies automatically in background process.

We could easily integrate Application Insights into our applications for different languages: .Net / Java / Java script….and support for different types: Service / Console / Web / Web API…

Take .Net Core Web API application for example. First you need create Application Insight resource for Azure portal under your subscription and get the instrumentation key. More detail please refer to this tutorial. Next, install Application Insights SDK for ASP.NET Core:

Microsoft.ApplicationInsights.AspNetCore

Create TelemetryClient and Initializer.

Setup application insights configurations from Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// configure key and...
services.Configure<TelemetryConfiguration>(
(o) => {
o.InstrumentationKey = "your-key";
o.TelemetryInitializers.Add(new CustomTelemetryInitializer("1.0", "Lab.AppInsights"));
});
// enable application insights
services.AddApplicationInsightsTelemetry();
services.AddSingleton<AzureMonitorManager>();
}

Then we are good to introduce Application Insights into our applications:

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
// try to send out some trace message to azure
_monitor.ThrowMessageTrace(nameof(Get), "Get WeatherForecast");
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new
WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToArray();
}

Start application and hit the API resource, then wait around 3–5 minutes and check application insights on the azure portal:

If you got problem to find out your trace event on the portal, you could check your sampling rate or IP restrictions (inside your configuration sections of Application Insights)

Quit easy? Isn’t it. Completed source example please refer here.

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)