This blog post was written by Anastasia Baranchenkova
In Visual Studio 2015 Update 1 Application Insights telemetry is available right from the Visual Studio. And you can even get it without providing a valid instrumentation key.
I created a sample application that I describe in details later on. In this application I forward WebApi framework traces to Application Insights (it is a similar thing that SergeyK described in one of his previous posts but it is done differently: by implementing
Full sample is here.
So if you start debugging web hosted WebApi application even if you do not provide instrumentation key you can see all the traces rights in your Diagnostics Tools window. You can search there, filter and see actual json file that would have been sent if you provided a valid instrumentation key:
For self-hosted WebApi applications you would need to configure ApplicationInsights providing a valid instrumentation key because Diagnostics Tools hub does not currently support this type of application. But you still can see all the telemetry from the VS itself. For that you need to open: View ->Other Windows->Application Insights Search. From there you connect to your Azure Subscription and get back all the telemetry. You can select different time intervals, filter by type or property values and see each telemetry item details:
And now I want to describe in details how you can forward WebApi traces to ApplicationInsigts to get all this beauty.
WebApi web-hosted application
- Create WebApi web hosted application. If you have Azure subscription check “Add ApplicationInsights to project”.
- If you did not add ApplicationInsights on application creation then add the latest Application Insights for Web nuget through nugget package manager. (In this case you do not have instrumentation key in the ApplicationInsights configuration file and no data will be sent to the portal but you can add it that later and for debugging purposes you have all you need)
- Add Microsoft.AspNet.WebApi.Tracing nuget package.
- Add ApplicationInsightsTraceWriter.cs
Trace gets trace message parameters and converts it to ApplicationInsights trace or exception.
1 2 3 4 5 6 7 8 9
CompositTraceWriter also implements
ITraceWriter and is used in case if some other
ITraceWriter is already registered by the application so that
ApplicationInsightsTraceWriter does not replace existing but rather is added to the list of trace writers.
- Add Extensions.cs
This class adds
HttpConfiguration.EnableApplicationInsights extension method that registers
- In Global.asax add
WebApi self-hosted application
For a self-hosted application you will do almost the same but
- Instead of Web nuget package add ApplicationInsights API nuget
- Create application insights resource manually through the portal and set instrumentation key programmatically
config.EnableApplicationInsights()from Startup.Configuration where config would be an instance of HttpConfiguration that you create in this method for other registration purposes.
I would highly encourage to also read this blog post that explains how and when to use WebApi tracing. The example above demonstrates basic integration points while in the article above you can find detailed information on the best practices of how to use WebApi tracing.