Scott Hanselman

Exploring Application Insights for disconnected or connected deep telemetry in ASP.NET Apps

October 19, 2016 Comment on this post [18] Posted in Azure
Sponsored By

Today on the ASP.NET Community Standup we learned about how you can use Application Insights in a disconnected scenario to get some cool - ahem - insights into your application.

Typically when someone sees Application Insights in the File | New Project dialog they assume it's a feature that only works in Azure, that will create some account for you and send your data to the cloud. While App Insights totally does do a lot of cool stuff when you have a cloud-hosted app and it does add a lot of value, it also supports a very useful "SDK only" mode that's totally offline.

Click "Add Application Insights to project" and then under "Send telemetry to" you click "Install SDK only" and no data gets sent to the cloud.

Application Insights dropdown - Install SDK only

Once you make your new project, can you learn more about AppInsights here.

For ASP.NET Core apps, Application Insights will include this package in your project.json - "Microsoft.ApplicationInsights.AspNetCore": "1.0.0" and it'll add itself into your middleware pipeline and register services in your Startup.cs. Remember, nothing is hidden in ASP.NET Core, so you can modify all this to your heart's content.

if (env.IsDevelopment())
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);

Request telemetry and Exception telemetry are added separately, as you like.

Make sure you show the Application Insights Toolbar by right-clicking your toolbars and ensuring it's checked.

Application Insights Dropdown Menu

The button it adds is actually pretty useful.

Application Insights Dropdown Menu

Run your app and click the Application Insights button.

NOTE: I'm using Visual Studio Community. That's the free version of VS you can get at I use it exclusively and I think it's pretty cool that this feature works just great on VS Community.

You'll see the Search window open up in VS. You can keep it running while you debug and it'll fill with Requests, Traces, Exceptions, etc.


I added a Exceptional case to /about, and here's what I see:

Searching for the last hours traces

I can dig into each issue, filter, search, and explore deeper:

Unhandled Exception

And once I've found something interesting, I can explore around it with the full details of the HTTP Request. I find the "telemetry 5 minutes before and after" query to be very powerful.

Track Operations

Notice where it says "dependencies for this operation?" That's not dependencies like "Dependency Injection" - that's larger system-wide dependencies like "my app depends on this web service."

You can custom instrument your application with the TrackDependancy API if you like, and that will cause your system's dependency to  light up in AppInsights charts and reports. Here's a dumb example as I pretend that putting data in ViewData is a dependency. It should be calling a WebAPI or a Database or something.

var telemetry = new TelemetryClient();

var success = false;
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
ViewData["Message"] = "Your application description page.";
telemetry.TrackDependency("ViewDataAsDependancy", "CallSomeStuff", startTime, timer.Elapsed, success);

Once I'm Tracking external Dependencies I can search for outliers, long durations, categorize them, and they'll affect the generated charts and graphs if/when you do connect your App Insights to the cloud. Here's what I see, after making this one code change. I could build this kind of stuff into all my external calls AND instrument the JavaScript as well. (note Client and Server in this chart.)

Application Insight Maps

And once it's all there, I can query all the insights like this:

Querying Data Live

To be clear, though, you don't have to host your app in the cloud. You can just send the telemetry to the cloud for analysis. Your existing on-premises IIS servers can run a "Status Monitor" app for instrumentation.

Application Insights Charts

There's a TON of good data here and it's REALLY easy to get started either:

  • Totally offline (no cloud) and just query within Visual Studio
  • Somewhat online - Host your app locally and send telemetry to the cloud
  • Totally online - Host your app and telemetry in the cloud

All in all, I am pretty impressed. There's SDKs for Java, Node, Docker, and ASP.NET - There's a LOT here. I'm going to dig deeper.

Sponsor: Big thanks to Telerik! 60+ ASP.NET Core controls for every need. The most complete UI toolset for x-platform responsive web and cloud development. Try now 30 days for free!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 19, 2016 15:23
Can the choice of "send telemetry to" be configured at install/runtime?

The scenario I'm thinking of is where I'd like use Insights in a web service that gets installed by customers, but want to have the ability to ask the customer whether they give permission for Insights data to be sent to the cloud or not.

October 19, 2016 16:13
Hey Scott, can you please clarify exactly how App Insights is configured for offline use and how the collected telemetry can later be forcibly sent to Azure? Your article was very vague on this point beyond initial installation. I have several applications where this feature would be extremely useful as my company migrates its systems to Azure.
October 19, 2016 17:31
Minor edit:

can you learn more about AppInsights here.

Under the first image
I think you mean

you can ........
October 19, 2016 17:54
great article! is it possible to add app insights to existing web app?
October 19, 2016 21:33
Is Application Insights supports Javascript / Ajax requests as well? I found some MSDN links, I couldn't get it from Azure portal.
October 19, 2016 21:43
@David Gardiner - Want to make sure I understand your question. Are you prompting your customers for permission to collect telemetry? Or are you distributing a web service to customers who want to ask their customers for permission?

@Alex Marshall - The offline functionality Scott showed is for using the App Insights tools against telemetry collected while you debug locally in VS. If you configure your app to send data to the App Insights service in Azure, then when you debug, your app will send telemetry to the cloud just like it would in production (whatever instrumentation key is being used by the App Insights SDK during your debug session). Sounds like you're looking for a different kind of offline support. Can you describe your scenario a bit more?

@Unbounded Context - Totally. Check out this doc.
October 19, 2016 21:48
@Anuraj - Yes, App Insights has a JS SDK you can add to your client-side to track those requests. These instructions should get you started, but let me know if you're running into issues.
October 20, 2016 0:38
@David Stephens

I've found that we miss lot of telemetry in AI server during high load periods. Do you have any articles/tips how diagnose this problem and fix?
October 20, 2016 1:39
@Unbounded Context
Yes it is possible to use App Insights for an existing Web App.
October 20, 2016 5:11
@David Stephens

It's the former. I have a web service that my customers install. I want to give them the option at install time (or post-install via configuration) to opt in or out of permitting the application to send analytics off-machine.
October 20, 2016 12:17
Since this has sdks for a number of frameworks, i assume that data is always read from .net core ?
October 20, 2016 20:49
@Bohdan Makohin - Telemetry from your service is likely being throttled when there's high data volume. There's more info about this sampling here. Contact me and we can work through your issue in more detail: daviste at microsoft dot com.

@David Gardiner - You can totally make this happen. You'll need to implement the send/don't send logic yourself as part of some telemetry abstraction in your app, based on the user's choice.

@Carlos Ferreira - There are App Insights SDKs for a bunch of different platforms, including .NET Core. If you're using the SDK, the code sending the telemetry to the App Insights service is part of your app itself, so there's no separate "reader".
October 21, 2016 13:41
I love the SDK, but is there a way to use an on premise collection server? Or interface with a different monitoring system, such as Zabbix or the ELK stack?
October 22, 2016 1:29
Why would I use this vs Glimpse?
October 23, 2016 2:49
How can I get browser-side JS telemetry into Visual Studio in an offline scenario?

When deployed, I push everything to the App Insights server, but for development I like everyone to keep their data isolated. No sense in everyone seeing exceptions for in-progress work that may not even get committed.

I'm loading JS AppInsights using the "snippet" technique and have an empty string for instrumentation key in the dev environment, but nothing is showing up in Visual Studio.
October 24, 2016 9:52
Thanks a lot. How to use this in existing webforms application?
October 25, 2016 1:14
@Amir - Yes. App Insights collects data from your service through a TelemetryChannel. You can implement custom TelemetryChannels to send telemetry to any storage you'd like, including ELK. Check out this blog post and this Github repo that implements custom TelemetryChannels.

@Martin - Glimpse is great for server + client profiling and diagnostics while you're developing locally. The App Insights features in VS that Scott showed fill similar roles, just in VS instead of the browser. The big distinction is having App Insights telemetry for your service in production in addition to while developing, across every instance of your service. You can view this production telemetry in the Azure Portal or VS.

@Jason - The browser-side JavaScript telemetry requires an instrumentation key (and therefore an App Insights resource in Azure), so it's not supported in local-only mode in VS. One way to separate your development and production telemetry is to create separate App Insights resources for each. They usually wind up in separate Azure resource groups, alongside databases and other resources you want to group together by "dev" or "prod." Then you'd need to switch out the instrumentation keys for those resources when you deploy, just like you would database connection strings. To change which resource the Visual Studio features read data from, right-click your project, and choose "Application Insights - Choose Telemetry Source."

@Ajax control toolkit - App Insights works with WebForms apps. If you're using Visual Studio 2015, right-click your project and choose "Add Application Insights Telemetry." Or, install the App Insights Web SDK; then in your Web.config, make sure under system.web there's an httpModule called "ApplicationInsightsWebTracking." Check out the documentation for more details.
October 25, 2016 2:11
Thanks @David_Stephens, it's a little disappointing that it's not possible to have browser-side telemetry in an offline environment, but I guess the optimistic way to look at it is that it's great we can have server-side telemetry in an offline environment. :)

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.